[AccessD] Timer etc.
Arthur Fuller
fuller.artful at gmail.com
Tue Sep 27 11:09:07 CDT 2022
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
More information about the AccessD
mailing list