[AccessD] Timer etc.

Stuart McLachlan stuart at lexacorp.com.pg
Tue Sep 27 16:34:12 CDT 2022


That result should have gone without saying.

The For...Next loop just required incrementing or decremeing a Register Variable, 
comparing it to a constant and jumping 
INC     EDX
CMP     EDX, 1000000
JLE     LP1  
That may well be optimised by a good compilerby loading EDX with 
100000  and then just two instructions:
DEC   EDX
JNE   LP1 

The While...Wend loop will have several extra steps to perform: with the variable  i -  
moving it in and out of registers, both when  incrementing  it and when comparing it to 
1000000.

As a side note, rounding Timer by declaring it as LONG is a strange thing to do. If you'd 
made it double or single , you could have got meaningful relative difference results in a 
fraction of a second with far fewer iterations.

 
On 27 Sep 2022 at 12:09, Arthur Fuller wrote:

> A few messages ago, I asked for advice on resolving times. Gustav and
> JWC (Colby) replied, and I got what I needed.
> 
> I'm teaching myself C++, and came across some code, which I decided to
> port to VBA with modifications. The point of the exercise was to
> compare the performance of a While Loop and a For/Next loop. On a
> small scale, the differences are unnoticable, so I upped the iteration
> count to one million. Then the difference is dramatic. The While test
> took 612 seconds; the For/Next loop took 190 seconds.
> 
> In case anyone is interested, here is the code:
> 
> <code>
> Option Explicit
> 
> Const CON_Limit = 1000000
> 
> Function WhileLoopTest()
>     Dim i As Long
>     i = 1
>     Dim timStart As Long, timStop As Long
>     timStart = Timer
>     'Debug.Print timStart
>     While i <= CON_Limit
>         Debug.Print i
>         i = i + 1
>     Wend
>     timStop = Timer
>     'Debug.Print timStop
>     'MsgBox "Elapsed time for " & CON_Limit & " iterations" & vbCrLf &
>     _ '    (timStop - timStart) & " seconds.", vbOKOnly, "While Test"
>     WhileLoopTest = (timStop - timStart)
> End Function
> 
> Function ForNextTest()
>     Dim i As Long
>     i = 1
>     Dim timStart As Long, timStop As Long
>     timStart = Timer
>     'Debug.Print "Start: " & timStart
>     For i = 1 To CON_Limit
>         Debug.Print i
>     Next
>     timStop = Timer
>     Debug.Print "Stop: " & timStop
>     'MsgBox "Elapsed time for " & CON_Limit & " iterations" & vbCrLf &
>     _ '    (timStop - timStart) & " seconds.", vbOKOnly, "For/Next
>     Test" ForNextTest = (timStop - timStart)
> 
> End Function
> 
> Sub DurationTest()
>     MsgBox "This program compares While loops with For/Next loops",
> vbOKOnly + vbInformation, _
>     "Timing Test"
>     Dim TimeW As Currency, TimeF As Currency
>     TimeW = WhileLoopTest()
>     TimeF = ForNextTest()
>     MsgBox "Results of comparison for " & CON_Limit & " iterations:" &
> vbCrLf & _
>         "While Loop: " & TimeW & " seconds." & vbCrLf & _
>         "For/Next  : " & TimeF & " seconds.", _
>         vbOKOnly + vbInformation, _
>         "While v. For/Next Comparison"
> End Sub
> </code>
> 
> -- 
> Arthur
> -- 
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
> 




More information about the AccessD mailing list