Which is the fastest executing VB loop construct? Do you have a favourite?
:cool:
Printable View
Which is the fastest executing VB loop construct? Do you have a favourite?
:cool:
Run below codes;)
Code:Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Dim i, j, k, temp, ret1, ret2, ret3 As Long
k = 1000000
''''''''''''''''''''
j = GetTickCount()
For i = 1 To k
temp = 3
Next
'MsgBox "For/Next:" & GetTickCount() - j
ret1 = GetTickCount() - j
''''''''''''''''''''
i = 1
j = GetTickCount()
Do While i <= k
temp = 3
i = i + 1
Loop
'MsgBox "Do/Loop:" & GetTickCount() - j
ret2 = GetTickCount() - j
'''''''''''''''''''''
i = 1
j = GetTickCount()
While i <= k
temp = 3
i = i + 1
Wend
ret3 = GetTickCount() - j
MsgBox "For/Next:" & ret1 & vbCrLf & "Do/Loop:" & ret2 & vbCrLf & "While/Wend:" & ret3
End Sub
dongaman, the worst part of your code is the Dim statement. It will only cast ret3 as Long, and the rest as Variant :rolleyes:
In VB you have to explicitly declare the data type of each variable, whether you use one Dim or separate Dim statements for them. If you don't declare it explicitly, it's cast as a Variant.
Where the loop involves a counter to be incremented, a Long type variable is preferred nowadays, since maybe today's processors and OSs can manipulate longs faster than ints.
.
:eek: :eek: :eek:
I found Syntax of Dim in MSDN:
Sorry for my careless:pCode:Dim [WithEvents] varname[([subscripts])] [As [New] type] [, [WithEvents] varname[([subscripts])] [As [New] type]] . . .
just one thing dongaman doesnt really have to do with the question just that when u put in code YOU use [ code ] you should use [ vbcode ]
Just a liitle hint :P
:) ;)
Thankfully, this counterintuitive structure has been fixed in dot net, and Dim the way Dongaman has it written behaves exactly as you would intuitively think (that is if you werent aware ...). I think everyone who has ever used VB has probably made this mistake ... I know I went quite some time without realizing ...Quote:
Originally posted by honeybee
In VB you have to explicitly declare the data type of each variable, whether you use one Dim or separate Dim statements for them. If you don't declare it explicitly, it's cast as a Variant.
.
To be sure that returned values are correct you need to do the same actions in them loops.Quote:
Originally posted by dongaman
Run below codes;)
Code:Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Dim i, j, k, temp, ret1, ret2, ret3 As Long
k = 1000000
''''''''''''''''''''
j = GetTickCount()
For i = 1 To k
temp = 3
Next
'MsgBox "For/Next:" & GetTickCount() - j
ret1 = GetTickCount() - j
''''''''''''''''''''
i = 1
j = GetTickCount()
Do While i <= k
temp = 3
i = i + 1
Loop
'MsgBox "Do/Loop:" & GetTickCount() - j
ret2 = GetTickCount() - j
'''''''''''''''''''''
i = 1
j = GetTickCount()
While i <= k
temp = 3
i = i + 1
Wend
ret3 = GetTickCount() - j
MsgBox "For/Next:" & ret1 & vbCrLf & "Do/Loop:" & ret2 & vbCrLf & "While/Wend:" & ret3
End Sub
Therefore the results are incorrect cause in the For/Next loop you're doing much less then in others.
Add a extra long to it t As long
Add a line in the for next loop t = t +1
and your results will be more correct.
hi,
for loop fastest as i know
The speed of loops depends on:
1. how fast arithmetic operations and compares proceed on the datatype controlling the loop
2. Overhead involved in processing control structures
This tested the fastest on this particular box out of:
While x > 0: x = x-1
For x =1 to 1000000
While x < 1000000 : x = x+ 1
x = x - 1 : if x > 0 then goto
I believe it is because of the type of compare.Code:Dim i As Long
i = 1000000
Do while i
i=i-1
Loop
It only requires one opcode to load the register and one to get the result vs two opcodes to load two registers and two for the result. The datatype for all of the operations was a long and either added to or subtracted from.
For ... next was out of contention all the way thru, as was
if ... then ... else goto <label>
for is da fastest... but that's because it doesn't always have to check the value or w/e... ahh!!! can't think or writ eor w/efds kfksfs d
Regarding Jim's Boolean compare loop post:
That is indeed interesting.
To bad that type of loop wouldnt be flexible enough for most situations (have to downcount your index and cant adjust the lower limit).
Interesting though ...
Look here for VBSpeed:
http://www.xbeat.net/vbspeed/
I think your link supports Jims explanation as to why his code example runs fasterQuote:
Quote:
A by-product of the tests above: True-conditions perform faster. So, if you can make assumptions about your conditions, set up the code so that the test returns True.
I know it's a nice page...
Before i written this codes in a minute,i already know which is the fastest loop.
But,I do not think so;)Quote:
Add a line in the for next loop t = t +1
and your results will be more correct.