Building
the
Class
This is a simple class so we are not going to use the
wizard.
Create a blank project, select Add Class Module, select Class Module and a
blank
Class Module will be created.
We need to declare the GetTickCount function and we need a few variables:
Private Declare Function GetTickCount
Lib
"kernel32" ()
As Long
Dim
StartTick, EndTick
As Long
' Variables to old the
start and end time value
Dim
started
As Boolean
'local variable(s) to
hold property value(s)
Private
mvarTime
As Long
'local copy
Public Time
As Long
' This variable will hold the
elapsed the time
' it will considered a Property of this class since
is declared as
' public, but its value can not be changed from the outside of the class
Now on the
Class_Initialize event we are going to initialize
the started variable to false so that
if we call the stop event of the timer before calling the start event of the
timer we know that the timer
was not started.
Private Sub
Class_Initialize()
started =
False
End Sub
Now the StartTimer, this is the function that that
will get the initial time, we need to set to true the
started variable so that we know that the timer was initiated.
Public Sub
StartTimer()
StartTick = GetTickCount()
EndTick = 0
started =
True
End Sub
Now the Stoptimer, if the started variable is set to
true then we can calculate the elapsed time by
subtracting from the EndTick the StartTick, the value is placed in the time
property of the class.
Public Sub Stoptimer()
If
started =
True Then
EndTick = GetTickCount()
Time = EndTick - StartTick
started =
False
Else
Time = 0
Exit
Sub
End If
End Sub
The code for the class is completed, it is a very small and simple class.
Where's a example of to use the class, lets assume that we named it
CPerfTimer.
Public Perf
As
CPerfTimer
Set
Perf =
New
CPerfTimer
Perf.StartTimer
' code to be benchmarked
' would be inserted here
Perf.Stoptimer
MsgBox Perf.Time & " milliseconds"
Set
Perf = Nothing
If you want to know the time in seconds divide the Perf.Time by 1000, if
you want to know the value
in minutes is should divide the Perf.Time by 60000.
[ Slow Timer! ] [ Building the Class ]
|