View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default For Next loop faster with counter after Next?

Hang on, are you saying now that it is faster without
the variable after the Next?

RBS


"Dave Peterson" wrote in message
...
I like to see the variable in the Next statement.

But I have seen Dana DeLouis do this:

For i = 0 to 10
'code
Next 'i

It's kind of the best of both worlds???

RB Smissaert wrote:

OK, thanks, 1% faster will be worth it for me and as you say it looks
better.
Couldn't see the speed difference, but I believe you.

RBS

"Bob Phillips" wrote in message
...
I would have expected it to be true, but not significantly so.

I did a 100 times repetitive loop of your code and found it to be
approx
1%
faster, which I think sounds about right.

But it is much nicer code IMO irrespective.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"RB Smissaert" wrote in message
...
Read in the book Visual Basic for Applications in 21 days by Matthew
Harris
(third edition) that putting the loop counter after the Next would
make
the
loop faster:

For i = 0 to 10
'code
Next i

I can see it makes the code clearer, but I didn't think it made it any
faster and on simple testing I can see no difference:

Option Explicit
Private lStartTime As Long
Private Declare Function timeGetTime Lib "winmm.dll" () As Long

Sub StartSW()
lStartTime = timeGetTime()
End Sub

Sub StopSW(Optional ByRef strMessage As Variant = "")
MsgBox "Done in " & timeGetTime() - lStartTime & " msecs", ,
strMessage
End Sub

Sub test()

Dim i As Long
Dim c As Long
Dim n As Long

StartSW

For i = 0 To 10000
For c = 0 To 1000
n = i + c
Next
Next

StopSW

End Sub


Is there any truth in this?


RBS





--

Dave Peterson