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

Home Posts Topics Members FAQ

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

ADezii
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
  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 19295
Denburt
1,356 Recognized Expert Top Contributor
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 Recognized Expert Expert
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
4601
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...
13
11228
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.
14
2030
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.
23
21984
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
1481
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
3757
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.
0
24161
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 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...
6
1586
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
0
1282
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...
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
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...
1
5325
isladogs
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...
0
4944
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...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1878
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
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
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.