'A class by Wossname, 2004
'A class aimed at programmers that
'require a way to test the
'speed and efficiency of their code or 'provide real-time performance
'figures in a finished app.
'A gift to all the frogs, dogs, stuffed 'toys, administrators and mods
'on the VBF boards that have helped me 'over the years. Cheers.
'Please use this class as you wish, just 'give me a credit in your prog
'somewhere :)
Public Class StopWatch
#Region "Member Data"
Private itsStarted As Integer
Private itsStopped As Integer
Private itsElapsed As Integer
Private itsState As StopWatchStates = StopWatchStates.HasNeverRun
#End Region
#Region "Enums"
Private Enum StopWatchStates As Integer
HasNeverRun = 0
IsRunningNow = 1
HasRunNowStopped = 2
End Enum
#End Region
#Region "Custom Exceptions"
Public Class StopWatchException
Inherits ApplicationException
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
End Class
#End Region
Public Sub StartTiming()
If itsState = StopWatchStates.IsRunningNow Then Throw New StopWatchException("StartTiming(): StopWatch already started!")
itsStarted = Environment.TickCount
itsState = StopWatchStates.IsRunningNow
End Sub
Public Sub StopTiming()
Select Case itsState
Case StopWatchStates.HasNeverRun
Throw New StopWatchException("StopTiming(): StopWatch not yet started!")
Case StopWatchStates.IsRunningNow
itsStopped = Environment.TickCount
itsElapsed = itsStopped - itsStarted
itsState = StopWatchStates.HasRunNowStopped
Case StopWatchStates.HasRunNowStopped
Throw New StopWatchException("StopTiming(): StopWatch already stopped!")
End Select
End Sub
Public Function Elapsed() As Long
Select Case itsState
Case StopWatchStates.HasNeverRun
Throw New StopWatchException("Elapsed(): StopWatch not yet started")
Case StopWatchStates.IsRunningNow
'the time passed up to this point
Return Environment.TickCount - itsStarted
Case StopWatchStates.HasRunNowStopped 'started and then stopped
Return itsElapsed 'return the time that passed while the timer WAS running
End Select
End Function
Public Function Running() As Boolean
Return itsState = StopWatchStates.IsRunningNow
End Function
Public ReadOnly Property StartedAt() As Long
Get
If itsState = StopWatchStates.HasNeverRun Then
Throw New StopWatchException("StartedAt(): StopWatch not yet started")
Else
Return itsStarted
End If
End Get
End Property
Public ReadOnly Property StoppedAt() As Long
Get
Select Case itsState
Case StopWatchStates.HasNeverRun
Throw New StopWatchException("StoppedAt(): StopWatch not yet started")
Case StopWatchStates.IsRunningNow
Throw New StopWatchException("StoppedAt(): StopWatch still running")
Case StopWatchStates.HasRunNowStopped
Return itsStopped
End Select
End Get
End Property
End Class
'sample code...
' Sub Main()
' Dim sw As StopWatch = New StopWatch
' Dim j As Long = 0
' sw.StartTiming()
' 'waste a bit of time
' For i As Integer = 1 To 10000000
' j += 1
' Wasteful(j)
' Next i
' sw.StopTiming()
' Console.WriteLine("Clock started at: {0}, stopped at: {1}, elapsed time: {2}", sw.StartedAt, sw.StoppedAt, sw.Elapsed)
' Console.ReadLine()
'End Sub
'Sub Wasteful(ByVal num As Long)
' 'do nothing
' If num > 54321 Then num = 54321
'End Sub