468,753 Members | 1,096 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

For...Next vs Do...Loop, and the Winner is?

ADezii
8,800 Expert 8TB
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 iterates a specified number of times, requires you to also implement or decrement some sort of Loop Counter, while a For...Next Loop does that work for you. Both Loops will provide the same results, but the For...Next Loop is substantially faster. One obvious point to mention is that you can't always replace a Do...Loop with a For...Next, you only want to use a For...Next if the number of Loop Iterations is fixed, and not based on some logical condition.

I've created some simple Benchmark Tests involving 3 trials comparing the efficiency of these two Loops using the timeGetTime() API Function. A comparative ratio of the two processes has also been calculated giving you a better viewpoint on the two comparisons. I'll post the code below for the Benchmark Tests. Should you have any questions, please feel free to ask them.
Expand|Select|Wrap|Line Numbers
  1. 'Declaration needed for API Call
  2. Public Declare Function timeGetTime Lib "winmm.dll" () As Long
Expand|Select|Wrap|Line Numbers
  1. Dim lngCounter As Long
  2. Dim lngStart_1 As Long
  3. Dim lngStart_2 As Long
  4. Dim lngStop_1 As Long
  5. Dim lngStop_2 As Long
  6. Dim varTestVariable As Variant
  7.  
  8. Const conNUM_OF_ITERATIONS As Long = 10000000
  9.  
  10. '******************************* 1st up, For...Next *******************************
  11. lngStart_1 = timeGetTime()
  12.  
  13.   For lngCounter = 1 To conNUM_OF_ITERATIONS
  14.     'do some processing here
  15.     varTestVariable = (lngCounter / 0.25) * 1.5
  16.   Next
  17. lngStop_1 = timeGetTime()
  18.  
  19. Debug.Print "For...Next took (" & FormatNumber((lngStop_1 - lngStart_1), 0) & _
  20.             ") milliseconds to execute the expression"
  21. '**********************************************************************************
  22.  
  23. '******************************* 2nd up, Do...Loop ********************************
  24. lngCounter = 1
  25.  
  26. lngStart_2 = timeGetTime()
  27.   Do Until lngCounter > conNUM_OF_ITERATIONS
  28.     'same processing here as for the For...Next Loop
  29.     varTestVariable = (lngCounter / 0.25) * 1.5
  30.       lngCounter = lngCounter + 1
  31.   Loop
  32. lngStop_2 = timeGetTime()
  33.  
  34. Debug.Print "Do...Loop took (" & FormatNumber((lngStop_2 - lngStart_2), 0) & _
  35.             ") milliseconds to execute the expression"
  36. '**********************************************************************************
  37.  
  38. Debug.Print "The For...Next Loop executed the same code in [" & _
  39.             Format((lngStop_1 - lngStart_1) / (lngStop_2 - lngStart_2), "Percent") & _
  40.             "] of the time it took Do...Loop!"
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. For...Next took (766) milliseconds to execute the expression
  2. Do...Loop took (1,515) milliseconds to execute the expression
  3. The For...Next Loop executed the same code in [50.56%] of the time it took Do...Loop!
  4.  
  5.  
  6. For...Next took (781) milliseconds to execute the expression
  7. Do...Loop took (1,516) milliseconds to execute the expression
  8. The For...Next Loop executed the same code in [51.52%] of the time it took Do...Loop!
  9.  
  10. For...Next took (781) milliseconds to execute the expression
  11. Do...Loop took (1,532) milliseconds to execute the expression
  12. The For...Next Loop executed the same code in [50.98%] of the time it took Do...Loop!
NOTE: The Access 2002 Developers Handbook stated that the For...Next Loop runs in about 45% of the time that Do...Loop runs for comparative code. My personal tests based on the above listed code segment, revealed a figure of approximately 51.02%, based on an Average of 3 trial runs.
May 27 '08 #1
2 18007
Denburt
1,356 Expert 1GB
Out of curiosity why didn't you mention the following?

While...Wend

I don't have time to set up your function and test it out right now or I would....
Jun 3 '08 #2
ADezii
8,800 Expert 8TB
Out of curiosity why didn't you mention the following?

While...Wend

I don't have time to set up your function and test it out right now or I would....
Hello Denburt. I didn't mention While...Wend because I was strictly referring to Looping Structures that can deal with a fixed number of iterations, as opposed to a Looping Structure terminating when a logical condition becomes True or False.
Jun 3 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

32 posts views Thread by Toby Newman | last post: by
14 posts views Thread by Ale K. | last post: by
23 posts views Thread by Mitchell Vincent | last post: by
13 posts views Thread by andreas | last post: by
4 posts views Thread by Neo | last post: by
reply views Thread by Semajthewise | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.