473,394 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Loop performance

I've been told that how one does a loop in VB .NET cna have significant speed
differernces. For example:

dim therows() as datarow
.....
dim row as datarow

for each row in therows
.....
next row
------------------------
vs.
-----------------------
dim therows() as datarow
.....
dim i as integer

for i = o to therows.getupperbound(0)
.....
next i

with the classic "for i=1 to next i" being much slower. Some testing on
really large string arrays confirms this. But we've heard otherwise, too.
And what about "while" loops, other ways of looping?

I suppose, too, it could have something to do with how data in an array,
rows in a dataset, etc, are indexed, not just how the counter gets
incremented.

Any articles, etc. on this?
Nov 21 '05 #1
4 1170
NormD,

Forget it about "significant speed differernces" if it is not really about
very long strings.

If we are talking about by instance datarows than it are parts of
nanoseconds. If it is in an UI application, than the paint on the window
from one label will with a normal sized table cost probably 1000000 times
more.

If it are strings have than a look what you do and if needed because you see
longtime operations, try to use the stringbuilder class. A string is
immutable and therefore has to be created new if there is done any change in
it. (Although there are some optimisations done in some methods so it is not
forever true).

I hope that this gives an idea.

Cor
Nov 21 '05 #2
Hi

Based on my test, it seems to be as the same speed as the other one
quantitatively.
NOTE: please do the test in long interval to elimitate the other effects.
e.g.
the form1_load will take a lot of time and memory to initialize.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim od As Long = DateTime.Now().Ticks
For i As Integer = 0 To strs.GetUpperBound(0)
s = strs(i)
Next
Button1.Text = (DateTime.Now().Ticks - od).ToString()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim od As Long = DateTime.Now().Ticks
For Each ss As String In strs
s = ss
Next
Button2.Text = (DateTime.Now().Ticks - od).ToString()
End Sub
Dim strs(1024 * 1024 * 10) As String
Dim s As String
Dim rd As New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Me.SqlDataAdapter1.Fill(Me.DataSet11)
'dt = Me.DataSet11.Tables(0)
For i As Integer = 0 To strs.GetUpperBound(0)
strs(i) = rd.Next().ToString()
Next
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #3
Thanks, guys.

On one hand I'd tend to agree with you that the difference should be very
small at most. On the other hand the person who discussed it with me is no
dummy and insists that the way they loop through their data makes a
substantial difference; ptactice, not theory. I will see if I can get the
specific example; I'm wondering is there is something else besides the loop
being changed, and that's what's making the diffference.

""Peter Huang" [MSFT]" wrote:
Hi

Based on my test, it seems to be as the same speed as the other one
quantitatively.
NOTE: please do the test in long interval to elimitate the other effects.
e.g.
the form1_load will take a lot of time and memory to initialize.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim od As Long = DateTime.Now().Ticks
For i As Integer = 0 To strs.GetUpperBound(0)
s = strs(i)
Next
Button1.Text = (DateTime.Now().Ticks - od).ToString()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim od As Long = DateTime.Now().Ticks
For Each ss As String In strs
s = ss
Next
Button2.Text = (DateTime.Now().Ticks - od).ToString()
End Sub
Dim strs(1024 * 1024 * 10) As String
Dim s As String
Dim rd As New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Me.SqlDataAdapter1.Fill(Me.DataSet11)
'dt = Me.DataSet11.Tables(0)
For i As Integer = 0 To strs.GetUpperBound(0)
strs(i) = rd.Next().ToString()
Next
End Sub

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #4
Hi

Ok, you are welcomed.
As for an Array, it is just a block of memory, so the index access is the
best approach, and the for each will implement the IEnumerator interface
for the Array class, and to the array class the concrete implement inside
should be the index access which is the same access mechanism.

On the other hand, in the Data Structure, another data structure is List,
which is a lots of seperated block memory, in this way, the IEnumerator
will win, because it just navigate through the List one by one, but use
Index will cause it repeated to access to the List.
e.g.
M1--->M2---->M3

This is a List.
What IEnumerator implement do is
1. M1
2. M2
3. M3

But for Index access, it may be as below.
1.M1
2. M1-->M2
3. M1-->M2-->M3

which is much slower.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
3
by: pertheli | last post by:
Hello, I have a large array of pointer to some object. I have to run test such that every possible pair in the array is tested. eg. if A,B,C,D are items of the array, possible pairs are AB, AC,...
10
by: Bhan | last post by:
I heard for(i=0;i<20;i++) { do-something; } Can be optimized. Is this can really optimized by an equivalent for loop or with while or do while loops?
8
by: Dave Veeneman | last post by:
In a for-loop, is a calculated expression re-calculated on each pass through the loop, or only once, when the loop is initialized? For example, assume the following loop: for (int i = 0; i <...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
3
by: cody | last post by:
Currently it is only legal to use types which has a method named GetEnumerator to be used in a foreach loop. This makes it impossible to use the same Enumerator after and before a foreach loop,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.