|
-
Mar 14th, 2003, 06:10 AM
#1
Thread Starter
Frenzied Member
Loops, a basic question of performance
Having this as refrence:
VB Code:
Dim i as integer
For i=0 to 1000
'Some code here
Next
Which one of the following will run faster? any as fast as the above?
1:
VB Code:
Dim i as Integer, J as Integer=1000
For i=0 to J
'Some code with no change in J value
Next
2:
VB Code:
Dim i as Integer, J as Integer=2000
For i=0 to J/2 ' Or any other operation on J
'Some code with no change in J value
Next
3:
VB Code:
Dim i as Integer
Const J as Integer=1000
For i=0 to J
'Some code with no change in J value
Next
4:
VB Code:
Dim i as Integer
Const J as Integer=2000
For i=0 to J/2 'or any other operation on J
'Some code with no change in J value
Next
-
Mar 14th, 2003, 06:55 AM
#2
Addicted Member
I know "1:" is faster than the reference, because it uses the value in cache.
I can't be absolutely sure about the other examples, though.
Make a search on optimiziation, I remember seeing a thread about this in VB General some months ago.
-
Mar 14th, 2003, 08:34 AM
#3
Member
-
Mar 14th, 2003, 08:38 AM
#4
Thread Starter
Frenzied Member
Well, timing is a good idea, but i liked to know the underlying process of each one.
-
Mar 14th, 2003, 11:35 AM
#5
Thread Starter
Frenzied Member
Here is the result of some timings:
considering the following methods:
Method1:
For I=0 to 1000000
Method2:
Dim J as Integer=1000000
For I=0 to J
Method3:
Dim J as Integer=20000000
For I=0 to J/2
Method4:
Const J as Integer=10000000
For I=0 to J
Method5:
Const J as Integer=20000000
For I= 0 to J/2
Results of each round is the result of going through each of the above loops 1000 times and calculating the average time measured in Ticks.
Round 1 2 3 Avg
-----------------------------------------------------------------------------------
Method1 393265.488 397070.960 394467.216 394934.5547
Method2 564712.016 564912.304 558302.800 562642.3733
Method3 468673.920 467372.048 463766.864 466604.2773
Method4 481392.208 480390.768 477286.304 479689.76
Method5 392664.624 390862.032 390962.176 391496.2773
-----------------------------------------------------------------------------------
Somehow strange results, no?
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Mar 14th, 2003, 11:55 AM
#6
-
Mar 15th, 2003, 10:15 AM
#7
Junior Member
They aren't accurate results; you can't change the loop amount, they _all_ must remain the same.
The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.
-
Mar 15th, 2003, 11:11 AM
#8
Thread Starter
Frenzied Member
Wold you please explain more? What you mean by loop amount? I know they can't be accurate but they are measured while the computer ran no extra application (besides system and some user underlying processes). However for sure the times to do a loop will vary depending on the system process that run each while.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Mar 16th, 2003, 11:39 PM
#9
Junior Member
Never mind, I didn't realise you where dividing them.
The arrow shot by the archer may, or may not, kill a single person. However, stratagems devised by a wise man, can kill even babes in the womb.
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
|