|
-
Nov 27th, 2002, 06:11 PM
#1
Thread Starter
PowerPoster
VB Loops... fastest typ?
Which is the fastest executing VB loop construct? Do you have a favourite?
-----------------------------------------
-RJ
[email protected]
-----------------------------------------
-
Nov 27th, 2002, 09:28 PM
#2
Fanatic Member
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
-
Nov 27th, 2002, 09:42 PM
#3
Well ...
dongaman, the worst part of your code is the Dim statement. It will only cast ret3 as Long, and the rest as Variant 
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.
.
-
Nov 27th, 2002, 09:53 PM
#4
Fanatic Member
-
Nov 27th, 2002, 10:23 PM
#5
Addicted Member
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
First person to be able to get what song is currently playing in Winamp 3.
-
Nov 27th, 2002, 10:33 PM
#6
Fanatic Member
-
Nov 28th, 2002, 05:54 AM
#7
PowerPoster
Re: Well ...
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.
.
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 ...
-
Nov 28th, 2002, 07:30 AM
#8
Frenzied Member
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
To be sure that returned values are correct you need to do the same actions in them loops.
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.
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Nov 28th, 2002, 10:06 AM
#9
Hyperactive Member
hi,
for loop fastest as i know
-
Nov 28th, 2002, 12:57 PM
#10
Frenzied Member
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
Code:
Dim i As Long
i = 1000000
Do while i
i=i-1
Loop
I believe it is because of the type of compare.
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>
-
Nov 28th, 2002, 03:25 PM
#11
Hyperactive Member
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
-
Nov 28th, 2002, 03:27 PM
#12
PowerPoster
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 ...
-
Nov 28th, 2002, 03:28 PM
#13
-
Nov 28th, 2002, 03:35 PM
#14
PowerPoster
I think your link supports Jims explanation as to why his code example runs faster
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.
-
Nov 28th, 2002, 03:36 PM
#15
I know it's a nice page...
-
Nov 28th, 2002, 08:49 PM
#16
Fanatic Member
Before i written this codes in a minute,i already know which is the fastest loop.
Add a line in the for next loop t = t +1
and your results will be more correct.
But,I do not think so
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|