473,383 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,383 developers and data experts.

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

ADezii
8,834 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 19268
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,834 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

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

Similar topics

32
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...
13
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...
14
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...
23
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
13
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
4
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...
0
ADezii
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...
6
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 ...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.