» Home » VB Fibre
Site
News
Files

Visual Basic
Strings
Math
General
Properties
Memory
Methods

Search
Testing

Inline ASM-VB
Strings
Math
General
Memory

Search
Using inline ASM
Submit!

While vs Exit Do

The difference is very small, although there seems to be one exception.

I've tested this with the following code:

Code:
Public Sub TestOne() Dim I As Long Do While Not I = 10000 I = I + 1 DoEvents Loop End Sub Public Sub TestTwo() Dim I As Long Do If I = 10000 Then Exit Do I = I + 1 DoEvents Loop End Sub

While % faster than Exit Do While (sec) Exit Do (sec)
0.3% 0.572686 0.571163
3.1% 0.564296 0.547285
0.1% 0.561068 0.560484
-0.5% 0.569387 0.572221
19.3% 0.563554 0.472231


User contributed notes:

Author: PRoPHEZZoR (Name_Here_No_Spam at HaCKERMAiL dot COM) Date: 11:08 24/08/2004
Pretty easy to explain... This particular WHILE loop should be slower than EXIT DO anyway, the first 3 outcomes are anomaly... Allow me to demonstrate... ;-)

WHILE calls these mnenomiks:
NOT
CMP
JE
(VB doesn't optimize jumps as far as I know)
IF/EXIT DO calls these
CMP
JE

see? less processor-drain...
Tell me that I'm right :-)

Author: PRoPHEZZoR (Name_Here_No_Spam at HaCKERMAiL dot COM) Date: 11:08 24/08/2004
Oops... I think those JEs were JNEs

Author: Comintern () Date: 22:03 15/03/2005
LOL. Not to mention that DoEvents would invalidate any type of time comparison that you could be making here. Each time through the loop, you are checking the Windows message queue, so every other process running has the potential to mess with the timings.

Author: Tom (hurendo_kun at hotmail dot com) Date: 15:05 31/05/2005
There are perfectly valid reasons to use Exit Do, and this is not one of them. Tools have a purpose, people! Don't use a screwdriver to eat your soup.

Author: Almar () Date: 19:05 31/05/2005
This site laos likes to demonstrate differences that people think exist. The difference is negligble on the amount of iterations used above.

Author: Tom (hurendo_kun at hotmail dot com) Date: 05:09 23/09/2005
Well, I wasn't referring to speed. Performance-wise, I can't think of any reason to use Exit Do in place of For..Next, but there are aesthetic reasons to make your code look nicer, and functional reasons so your logic doesn't look like spaghetti. I am a strong advocate of readable code. ;)

Author: TheShau (shauros at walla dot com) Date: 15:08 04/08/2006
Why did you put DoEvents??
Comintern is very right, The resaults above mean nothing because of the DoEvents.

I tested this:

Public Sub TestOne()
Dim I As Long
Do While Not I = 10000
I = I + 1
Loop
End Sub

Public Sub TestTwo()
Dim I As Long
Do
If I = 10000 Then Exit Do
I = I + 1
Loop
End Sub

Public Sub TestThree()
Dim I As Long
Do Until I = 10000
I = I + 1
Loop
End Sub

It seems that the third was the fastest and the second slowest, And it seems reasonable because in the second test there might be two jumps instead of one. One uncoditional and one for escaping. While the others can be written with one jump.

Add user-note
Author:
E-mail (optional):
Anti spam, please enter 'ok' without quotes:
Comment: