VB Code:
  1. 'A class by Wossname, 2004
  2.  
  3. 'A class aimed at programmers that
  4. 'require a way to test the
  5. 'speed and efficiency of their code or 'provide real-time performance
  6. 'figures in a finished app.
  7.  
  8. 'A gift to all the frogs, dogs, stuffed 'toys, administrators and mods
  9. 'on the VBF boards that have helped me 'over the years.  Cheers.
  10.  
  11.  
  12.  
  13. 'Please use this class as you wish, just 'give me a credit in your prog
  14. 'somewhere :)
  15.  
  16. Public Class StopWatch
  17.  
  18. #Region "Member Data"
  19.     Private itsStarted As Integer
  20.     Private itsStopped As Integer
  21.     Private itsElapsed As Integer
  22.     Private itsState As StopWatchStates = StopWatchStates.HasNeverRun
  23. #End Region
  24. #Region "Enums"
  25.     Private Enum StopWatchStates As Integer
  26.         HasNeverRun = 0
  27.         IsRunningNow = 1
  28.         HasRunNowStopped = 2
  29.     End Enum
  30. #End Region
  31. #Region "Custom Exceptions"
  32.     Public Class StopWatchException
  33.         Inherits ApplicationException
  34.         Public Sub New(ByVal message As String)
  35.             MyBase.New(message)
  36.         End Sub
  37.     End Class
  38. #End Region
  39.  
  40.     Public Sub StartTiming()
  41.         If itsState = StopWatchStates.IsRunningNow Then Throw New StopWatchException("StartTiming(): StopWatch already started!")
  42.         itsStarted = Environment.TickCount
  43.         itsState = StopWatchStates.IsRunningNow
  44.     End Sub
  45.  
  46.     Public Sub StopTiming()
  47.         Select Case itsState
  48.             Case StopWatchStates.HasNeverRun
  49.                 Throw New StopWatchException("StopTiming(): StopWatch not yet started!")
  50.  
  51.             Case StopWatchStates.IsRunningNow
  52.                 itsStopped = Environment.TickCount
  53.                 itsElapsed = itsStopped - itsStarted
  54.                 itsState = StopWatchStates.HasRunNowStopped
  55.  
  56.             Case StopWatchStates.HasRunNowStopped
  57.                 Throw New StopWatchException("StopTiming(): StopWatch already stopped!")
  58.         End Select
  59.     End Sub
  60.  
  61.     Public Function Elapsed() As Long
  62.         Select Case itsState
  63.             Case StopWatchStates.HasNeverRun
  64.                 Throw New StopWatchException("Elapsed(): StopWatch not yet started")
  65.  
  66.             Case StopWatchStates.IsRunningNow
  67.                 'the time passed up to this point
  68.                 Return Environment.TickCount - itsStarted
  69.  
  70.             Case StopWatchStates.HasRunNowStopped           'started and then stopped
  71.                 Return itsElapsed                 'return the time that passed while the timer WAS running
  72.         End Select
  73.     End Function
  74.  
  75.     Public Function Running() As Boolean
  76.         Return itsState = StopWatchStates.IsRunningNow
  77.     End Function
  78.  
  79.     Public ReadOnly Property StartedAt() As Long
  80.         Get
  81.             If itsState = StopWatchStates.HasNeverRun Then
  82.                 Throw New StopWatchException("StartedAt(): StopWatch not yet started")
  83.             Else
  84.                 Return itsStarted
  85.             End If
  86.         End Get
  87.     End Property
  88.  
  89.     Public ReadOnly Property StoppedAt() As Long
  90.         Get
  91.             Select Case itsState
  92.                 Case StopWatchStates.HasNeverRun
  93.                     Throw New StopWatchException("StoppedAt(): StopWatch not yet started")
  94.  
  95.                 Case StopWatchStates.IsRunningNow
  96.                     Throw New StopWatchException("StoppedAt(): StopWatch still running")
  97.  
  98.                 Case StopWatchStates.HasRunNowStopped
  99.                     Return itsStopped
  100.             End Select
  101.         End Get
  102.     End Property
  103.  
  104. End Class
  105.  
  106. 'sample code...
  107.     '   Sub Main()
  108.  
  109.     '   Dim sw As StopWatch = New StopWatch
  110.     '   Dim j As Long = 0
  111.  
  112.     '   sw.StartTiming()
  113.  
  114.     '   'waste a bit of time
  115.     '   For i As Integer = 1 To 10000000
  116.     '       j += 1
  117.     '       Wasteful(j)
  118.     '   Next i
  119.  
  120.     '   sw.StopTiming()
  121.  
  122.     '   Console.WriteLine("Clock started at: {0}, stopped at: {1}, elapsed time: {2}", sw.StartedAt, sw.StoppedAt, sw.Elapsed)
  123.     '   Console.ReadLine()
  124.  
  125.     'End Sub
  126.  
  127.     'Sub Wasteful(ByVal num As Long)
  128.     '   'do nothing
  129.     '   If num > 54321 Then num = 54321
  130.     'End Sub

Also: You can query the Elapsed property after starting the stopwatch so you can get a running count of the time elapsed.

Simply put, the above class gives you millisecond accurate time keeping info about your app's speed during loops or timeouts etc...

Hope you find it useful, I certainly do.

If you manage to find a bug in it (IE, an exception of type other than StopWatchException, then please let me know .

Cheers.