473,545 Members | 1,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

8,834 Recognized Expert Expert
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
  8. Const conNUM_OF_ITERATIONS As Long = 10000000
  10. '******************************* 1st up, For...Next *******************************
  11. lngStart_1 = timeGetTime()
  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()
  19. Debug.Print "For...Next took (" & FormatNumber((lngStop_1 - lngStart_1), 0) & _
  20.             ") milliseconds to execute the expression"
  21. '**********************************************************************************
  23. '******************************* 2nd up, Do...Loop ********************************
  24. lngCounter = 1
  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()
  34. Debug.Print "Do...Loop took (" & FormatNumber((lngStop_2 - lngStart_2), 0) & _
  35.             ") milliseconds to execute the expression"
  36. '**********************************************************************************
  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!"
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!
  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!
  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 19295
1,356 Recognized Expert Top Contributor
Out of curiosity why didn't you mention the following?


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


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

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

Similar topics

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 where the loop will be traversed a fixed number of times. It is very flexible, and novice programmers should take care not to abuse the power it...
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch statement? The break keyword will only come out of the switch, not the for-next loop.
by: Ale K. | last post by:
i know that For...Next as a Exit For.... there is any way from the middle of my for...next code to go to the next item and jump out part of my code , like the same thing that can be done with exit for, but instead for go to the next item?? Thanks.
by: Mitchell Vincent | last post by:
Is there any way to "skip" iterations in a for loop? Example : for x = 1 to 10 if something = 1 next endif
by: andreas | last post by:
Hi, I want to do some calculation like ( t1 and t2 are known) for i = t1 to t2 for j = t1 to t2 ..... ..... for p = t1 to t2 for q = t1 to t2
by: Neo | last post by:
I found on error resume next doesn't work in for each... e.g. on error resume next for each x in y 'do stuff next if you have an error in for each loop, it falls in infinite loop... it doesn't move to next element... this sad thing but, it's indication that we must move to try catch instead of on error.
by: ADezii | last post by:
If you want to visit each item in an Array, you have two alternatives: Use a For Each..Next loop, using a Variant to retrieve each value in turn. Use a For...Next loop, looping from the Lower Bound to the Upper Bound of the Array. For Each...Next seems simpler because you need not worry about retrieving the Lower and Upper Bounds- the...
by: cargo | last post by:
Hello I have a SQL problem that I not sure of how to categorise. I have a table like this - 3 columns (Col_1,Col_2,Col_3) and want to create a 4th column =New_Column Col_1 Col_2 Col_3 New_Column 1 3 15.3 0.3 = 15.3-15.0 1 2 15.8 0.8 15.8-15.0
by: Semajthewise | last post by:
Hi all. I'm starting on my next part of my teach myself vb program. What I am trying to do is on button click open a textfile showing the math as it would be done long hand. I started writing the code and now I think There might be a better way of doing it than what I have been doing. Here's the code as far as it goes. Private Sub...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.