For loop

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

Is there any difference between using:

For Each message As String In Messages
....
Next message

Or

For Each message As String In Messages
....
Next

The difference is between adding the variable after Next.

I am using ASP.NET 2.0

Thanks,

Miguel
 
shapper said:
Hello,

Is there any difference between using:

For Each message As String In Messages
...
Next message

Or

For Each message As String In Messages
...
Next

The difference is between adding the variable after Next.

I am using ASP.NET 2.0

Thanks,

Miguel

No, there is no difference in the final code produced. You can add the
variable name so that it's more visible what loop the Next command is
ending.
 
There is no logical difference, however the second sample may execute more
efficiently.
 
There is no logical difference, however the second sample may execute more
efficiently.


I think this really depends on the number of items in the loop

I've tried to execute the following code and the first loop with
omitted element in the ForEach/Next statement is faster for me...

Dim tmrStart As Double, tmrElapsed As Double
Dim test As Double

test = 0

Dim testCollection As New Collection

For i = 0 To 10000000
testCollection.Add(test)
Next

tmrStart = Now().ToOADate

For Each testObject As Double In testCollection
test += 10
Next

tmrElapsed = Now().ToOADate - tmrStart
Debug.Print("exit: " & tmrElapsed)

test = 0
tmrStart = Now().ToOADate

For Each testObject As Double In testCollection
test += 10
Next testObject

tmrElapsed = Now().ToOADate - tmrStart
Debug.Print("exit: " & tmrElapsed)
 
Alexey said:
I think this really depends on the number of items in the loop

I've tried to execute the following code and the first loop with
omitted element in the ForEach/Next statement is faster for me...

Dim tmrStart As Double, tmrElapsed As Double
Dim test As Double

test = 0

Dim testCollection As New Collection

For i = 0 To 10000000
testCollection.Add(test)
Next

tmrStart = Now().ToOADate

For Each testObject As Double In testCollection
test +=

tmrElapsed = Now().ToOADate - tmrStart
Debug.Print("exit: " & tmrElapsed)

test = 0
tmrStart = Now().ToOADate

For Each testObject As Double In testCollection
test += 10
Next testObject

tmrElapsed = Now().ToOADate - tmrStart
Debug.Print("exit: " & tmrElapsed)

The system clock has too low resolution to give any reliable result in a
test that runs for such a short time. The resolution is about 16 ms,
which means that the start time and end time can each have an inaccuracy
of up to 16 ms, giving a total of up to 32 ms. Did you observe any time
difference greater than that?

Most of the time I get the same execution time for both.

Examining the final machine code that is created, I see that the code
for the loops are identical (except for the memory addresses of course):

For Each testObject As Double In testCollection
000000f4 mov ecx,dword ptr [ebp+FFFFFF44h]
000000fa cmp dword ptr [ecx],ecx
000000fc call 59EB0D38
00000101 mov esi,eax
00000103 mov dword ptr [ebp+FFFFFF40h],esi
00000109 nop
0000010a jmp 00000137
0000010c mov ecx,dword ptr [ebp+FFFFFF40h]
00000112 call dword ptr ds:[03AB00A0h]
00000118 mov esi,eax
0000011a mov ecx,esi
0000011c call 59ECA994
00000121 fstp qword ptr [ebp-70h]
00000124 fld qword ptr [ebp-70h]
00000127 fstp qword ptr [ebp-48h]
test += 10
0000012a fld dword ptr ds:[04581CE0h]
00000130 fadd qword ptr [ebp-30h]
00000133 fstp qword ptr [ebp-30h]
Next testObject
00000136 nop
00000137 mov ecx,dword ptr [ebp+FFFFFF40h]
0000013d call dword ptr ds:[03AB009Ch]
00000143 mov esi,eax
00000145 test esi,esi
00000147 jne 0000010C
00000149 nop
0000014a nop
0000014b mov dword ptr [ebp-1Ch],0
00000152 mov dword ptr [ebp-18h],0FCh
00000159 push 4581CC8h
0000015e jmp 00000160
00000160 mov edx,dword ptr [ebp+FFFFFF40h]
00000166 mov ecx,7910C914h
0000016b call 7596A294
00000170 test eax,eax
00000172 je 0000018D
00000174 mov edx,dword ptr [ebp+FFFFFF40h]
0000017a mov ecx,7910C914h
0000017f call 7596A294
00000184 mov ecx,eax
00000186 call dword ptr ds:[03AB00B0h]
0000018c nop
0000018d nop
0000018e pop eax
0000018f jmp eax



vs.


For Each testObject As Double In testCollection
00000236 mov ecx,dword ptr [ebp+FFFFFF44h]
0000023c cmp dword ptr [ecx],ecx
0000023e call 59EB0D38
00000243 mov esi,eax
00000245 mov dword ptr [ebp+FFFFFF3Ch],esi
0000024b nop
0000024c jmp 0000027F
0000024e mov ecx,dword ptr [ebp+FFFFFF3Ch]
00000254 call dword ptr ds:[03AB00A8h]
0000025a mov esi,eax
0000025c mov ecx,esi
0000025e call 59ECA994
00000263 fstp qword ptr [ebp+FFFFFF68h]
00000269 fld qword ptr [ebp+FFFFFF68h]
0000026f fstp qword ptr [ebp-50h]
test += 10
00000272 fld dword ptr ds:[04581CE4h]
00000278 fadd qword ptr [ebp-30h]
0000027b fstp qword ptr [ebp-30h]
Next
0000027e nop
0000027f mov ecx,dword ptr [ebp+FFFFFF3Ch]
00000285 call dword ptr ds:[03AB00A4h]
0000028b mov esi,eax
0000028d test esi,esi
0000028f jne 0000024E
00000291 nop
00000292 nop
00000293 mov dword ptr [ebp-1Ch],0
0000029a mov dword ptr [ebp-18h],0FCh
000002a1 push 4581CBCh
000002a6 jmp 000002A8
000002a8 mov edx,dword ptr [ebp+FFFFFF3Ch]
000002ae mov ecx,7910C914h
000002b3 call 7596A294
000002b8 test eax,eax
000002ba je 000002D5
000002bc mov edx,dword ptr [ebp+FFFFFF3Ch]
000002c2 mov ecx,7910C914h
000002c7 call 7596A294
000002cc mov ecx,eax
000002ce call dword ptr ds:[03AB00ACh]
000002d4 nop
000002d5 nop
000002d6 pop eax
000002d7 jmp eax


Which I submit as proof to what I said in my first post.
 
The system clock has too low resolution to give any reliable result in a
test that runs for such a short time. The resolution is about 16 ms,
which means that the start time and end time can each have an inaccuracy
of up to 16 ms, giving a total of up to 32 ms. Did you observe any time
difference greater than that?

No, I didn't. I don't think that it's really important to find if the
second approach will be a little bit faster, or not, because, in
general, IMO the most cause for most performance problems is somewhere
else...
 
Alexey said:
No, I didn't. I don't think that it's really important to find if the
second approach will be a little bit faster, or not, because, in
general, IMO the most cause for most performance problems is somewhere
else...

Yes, you are right, it usually doesn't make a noticeable difference, and
one should not spend time optimising code that doesn't have performance
problems.

On the other hand, knowing what's efficient and what's not will help you
avoid the really inefficient code, and to more easily write efficient
code in the few places where it actually makes a difference.
 
Back
Top