Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default For Next loop faster with counter after Next?

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default For Next loop faster with counter after Next?

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




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default For Next loop faster with counter after Next?

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





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default For Next loop faster with counter after Next?

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
  #5   Report Post  
Posted to microsoft.public.excel.programming
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




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default For Next loop faster with counter after Next?

I put your code within a loop of 50 and ran each case 4 times. I saw no
difference. It varied by run which was faster, but the difference was never
more than 2/10ths of a percent.

--
Regards,
Tom Ogilvy


"RB Smissaert" wrote in message
...
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




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default For Next loop faster with counter after Next?

From what I've read (I've never tested), Next without the variable is faster.



RB Smissaert wrote:

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


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 300
Default For Next loop faster with counter after Next?

On Sun, 27 Aug 2006, RB Smissaert wrote:

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:


I can't imagine why it would make a difference. The first time the code is
run it gets pseudo-compiled. Both would compile to the same thing. Any
difference you might get (like the 1%) would come from one case having to
compile first, and the other case not having to. So before any comparison
you should press Debug - Compile to put both on equal footing.

Don <www.donwiss.com (e-mail link at home page bottom).
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default For Next loop faster with counter after Next?

I did compile Jim's code before running both options and still I had the
same difference as he had.
For some reason now however I can't reproduce that anymore.
I agree that logically one would expect it to be the same and I think now
that it is indeed the same.
So, luckily I didn't waste an hour's work then!

RBS


"Don Wiss" wrote in message
...
On Sun, 27 Aug 2006, RB Smissaert wrote:

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:


I can't imagine why it would make a difference. The first time the code is
run it gets pseudo-compiled. Both would compile to the same thing. Any
difference you might get (like the 1%) would come from one case having to
compile first, and the other case not having to. So before any comparison
you should press Debug - Compile to put both on equal footing.

Don <www.donwiss.com (e-mail link at home page bottom).


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default For Next loop faster with counter after Next?

I think perhaps you are all testing with nicely structured code. If you
sprinkled a few ifs and gotos around, wandering all over the place, like us
old fashioned real programmers used to do, the the compiler may not be able
to link a particular NEXT with its matching FOR without the variable
specified, and have to generate more complex code.

just a theory


"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






  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default For Next loop faster with counter after Next?

If it can't, then it won't be able to run the code.

If's and goto's still have rules that the compiler has to interpret.

Just another thought.

--
Regards,
Tom Ogilvy

"David Cox" wrote in message
...
I think perhaps you are all testing with nicely structured code. If you
sprinkled a few ifs and gotos around, wandering all over the place, like us
old fashioned real programmers used to do, the the compiler may not be able
to link a particular NEXT with its matching FOR without the variable
specified, and have to generate more complex code.

just a theory


"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






  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default For Next loop faster with counter after Next?

I had thought about that, but I have tested with a very complex For Next
loop and I still couldn't see the difference.
As Tom says it would be bad news if the compiler would be thrown by that.
Clever bits of software these compilers, especially at the speed they do it.

RBS

"David Cox" wrote in message
...
I think perhaps you are all testing with nicely structured code. If you
sprinkled a few ifs and gotos around, wandering all over the place, like us
old fashioned real programmers used to do, the the compiler may not be able
to link a particular NEXT with its matching FOR without the variable
specified, and have to generate more complex code.

just a theory


"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





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Faster way to loop through two ranges YH Excel Programming 5 August 26th 06 05:17 PM
Is there a faster loop than this Andibevan Excel Programming 4 August 25th 06 03:27 PM
Counter variable in For Loop [email protected] Excel Programming 3 June 8th 06 06:56 PM
Should I use Do-While loop for my record counter? excelnut1954 Excel Programming 0 March 24th 06 09:25 PM
Faster For-Next Loop? [email protected] Excel Programming 3 January 7th 05 09:08 PM


All times are GMT +1. The time now is 09:02 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"