473,503 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

For loop

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

Feb 25 '07 #1
6 1352
shapper wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Feb 25 '07 #2
There is no logical difference, however the second sample may execute more
efficiently.

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net
"shapper" <md*****@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
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
Feb 26 '07 #3
On Feb 26, 11:38 am, "Steve C. Orr [MCSD, MVP, CSM, ASP Insider]"
<S...@Orr.netwrote:
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)

Feb 26 '07 #4
Alexey Smirnov wrote:
On Feb 26, 11:38 am, "Steve C. Orr [MCSD, MVP, CSM, ASP Insider]"
<S...@Orr.netwrote:
>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 +=

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.

--
Göran Andersson
_____
http://www.guffa.com
Feb 26 '07 #5
On Feb 26, 6:43 pm, Göran Andersson <g...@guffa.comwrote:
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...

Feb 26 '07 #6
Alexey Smirnov wrote:
On Feb 26, 6:43 pm, Göran Andersson <g...@guffa.comwrote:
>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...
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.

--
Göran Andersson
_____
http://www.guffa.com
Feb 26 '07 #7

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

Similar topics

0
2911
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
5244
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
43
5527
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
7282
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
32
4593
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
2
2658
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
3
3504
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
2554
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
16
3490
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
2
19288
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
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
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7342
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...
1
6998
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...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.