Loop performance

G

Guest

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?
 
C

Cor Ligthert [MVP]

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
 
P

Peter Huang [MSFT]

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.
 
G

Guest

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.
 
P

Peter Huang [MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top