About This Code
Brief Description:
LSTimer class . . .
Contributor:
Dallas Gimpel
Notes Version:
R5.x, R6.x, R7.x
Last Modified:
07 Jun 2007
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
%REM
Class name: LSTimer
Description: Just a utility class for timing code execution.
Author: Dallas Gimpel
Date: 06/15/2003
%END REM
Public Class LSTimer
Private dblTickStart As Double
Private dblTickEnd As Double
Private intTicksPerSecond As Integer
'+++ CONSTRUCTOR +++
Public Sub New()
Const LSI_THREAD_TICKS_PER_SEC = 7 '// from lsprcval.lss
Me.intTicksPerSecond% = Getthreadinfo(LSI_THREAD_TICKS_PER_SEC)
End Sub
'+++ PUBLIC METHODS +++
Public Sub StartTimer()
'// sets/resets the starting tick count
Me.dblTickStart# = Getthreadinfo(Me.LSI_THREAD_TICKS)
End Sub
Public Sub StopTimer()
'// sets/resets the ending tick count
Me.dblTickEnd# = Getthreadinfo(Me.LSI_THREAD_TICKS)
End Sub
'+++ PUBLIC PROPERTIES +++
Public Property Get GetStartTick As Double
GetStartTick# = Me.dblTickStart#
End Property
Public Property Get GetEndTick As Double
GetEndTick# = Me.dblTickEnd#
End Property
Public Property Get GetSecondsLapsed As Double
'// Calculates the time lapsed in seconds.
'// On Win/32, for example, it should be accurate to the millisecond.
'// The "tick count" is the number of clock "ticks" since booting (it starts at 0 after booting).
GetSecondsLapsed# = Me.dblTickEnd# - Me.dblTickStart#
GetSecondsLapsed# = GetSecondsLapsed# / Me.intTicksPerSecond%
End Property
'+++PRIVATE PROPERTIES +++
Private Property Get LSI_THREAD_TICKS As Integer
LSI_THREAD_TICKS = 6 '// from lsprcval.lss
End Property
End Class
Usage / Example
This code is just my stab at providing a way to accurately time the total execution time for a given block of code. I'm sure it's not unique, but I didn't see anything similar here and I thought it might be useful for someone else.
Sample usage:
Const TEST_VALUE& = 10000& * 6&
Dim lsTimer As New LSTimer()
Dim x As Long
Dim strMsgTxt As String
x = 0
Call lsTimer.StartTimer()
While x < TEST_VALUE
Wend
Call lsTimer.StopTimer()
Print "Starting tick: " & Format(lsTimer.GetStartTick, "0,000")
strMsgTxt$ = "Time lapsed: " & Format(lsTimer.GetSecondsLapsed(), "0.000")
Msgbox strMsgTxt$, , "DEBUG . . ."
Comments
Posted by Andre Guirard on 06/23/2007 10:33:36 AMIt doesn't seem like a complex enough task...
...for a class to be worth while. You added 40 lines of code to avoid declaring one extra variable and having to type (a-b)/Getthreadinfo(LSI_THREAD_TICKS_PER_SEC).
You are assuming that the timer increases by one on each tick. In fact, on Windows, it seems to increase in increments of 16 or 17. Using GetThreadInfo is a little more accurate than using Timer, but it's not millisecond accurate, as this code shows.
Dim st1 As Double, st2 As Double, st3 As Double, iters As Long
st1 = Getthreadinfo(6)
' start on a fresh tick.
Do
st2 = Getthreadinfo(6)
Loop Until st1 <> st2
Do
st1 = Getthreadinfo(6)
iters = iters + 1
Loop Until st1 <> st2
Print "time increased by " & (st1-st2) & " ticks over " & iters & " iterations."