|
| Self Averaging High Resolution Performance Counter Class |
|
|
|
|
| Messages |
|
Related Types |
This message was discovered on ASPFriends.com 'aspngspeed' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| Kloberg Mac (LAM) (VIP) |
This might be useful for anybody trying to measure/benchmark certain problems or coding techniques on the .NET platform. The class shown below is a self averaging timer that relies on hardware and therefore yields very precise results.
Enjoy! :-) -- Sincerely, Mac Kloberg LIEBHERR AMERICA INC. http://www.liebherr-us.com
'********************************************************************* ' DotNet Self Averaging High Resolution Performance Timer ' 8/10/2001 Mac Kloberg (mailto:Click here to reveal e-mail address) '********************************************************************* Public Class HiResPerformanceTimer Declare Function QueryPerformanceCounter Lib "Kernel32.dll" (ByRef lpPerformanceCount As UInt64) As Long Declare Function QueryPerformanceFrequency Lib "Kernel32.dll" (ByRef lpFrequency As UInt64) As Long
Private _TimerOverhead As Decimal Private _Precision As Decimal Private _TickFrequency As UInt64 Private _StartTick As UInt64 Private _CurrentTick As UInt64 Private _StopTick As UInt64 Private _Elapsed As UInt64 Private _IterationCounter As Integer = 1 Private _ElapsedAccu As Decimal Private _IsRunning As Boolean = False
Private iTmp1 As Integer Private dTmp1 As Decimal
Public ReadOnly Property TickFrequency() As Decimal Get TickFrequency = System.Convert.ToDecimal(_TickFrequency) End Get End Property
Public ReadOnly Property TimerOverhead() As Decimal Get TimerOverhead = _TimerOverhead End Get End Property
Public ReadOnly Property StartTick() As Decimal Get StartTick = System.Convert.ToDecimal(_StartTick) End Get End Property
Public ReadOnly Property CurrentTick() As Decimal Get QueryPerformanceCounter(_CurrentTick) CurrentTick = System.Convert.ToDecimal(_CurrentTick) End Get End Property
Public ReadOnly Property StopTick() As Decimal Get StopTick = System.Convert.ToDecimal(_StopTick) End Get End Property
Public ReadOnly Property Elapsed() As Decimal Get If _IsRunning Then Elapsed = (CurrentTick - StartTick) / TickFrequency Else Elapsed = (StopTick - StartTick - TimerOverhead) / TickFrequency End If End Get End Property
Public ReadOnly Property AvgElapsed() As Decimal Get AvgElapsed = System.Convert.ToDecimal(_ElapsedAccu) / _IterationCounter End Get End Property
Public ReadOnly Property Precision() As Decimal Get Precision = 1 / TickFrequency End Get End Property
Public ReadOnly Property Iterations() As Integer Get Iterations = _IterationCounter End Get End Property
Public ReadOnly Property IsRunning() As Boolean Get IsRunning = _IsRunning End Get End Property
Public Sub Start() _IsRunning = True QueryPerformanceCounter(_StartTick) End Sub
Public Sub [Stop]() QueryPerformanceCounter(_StopTick) _IsRunning = False _IterationCounter += 1 _ElapsedAccu += Elapsed End Sub
Public Sub AvgReset() _ElapsedAccu = 0 _IterationCounter = 1 End Sub
Public Sub New() 'Get Tick Frequency If QueryPerformanceFrequency(_TickFrequency) = 0 Then Throw New Exception("Error: This hardware platform doesn't support high-resolution performance counters!") Else 'Measure our own Overhead across 1000 Start/Stops dTmp1 = 0 For iTmp1 = 1 To 1000 Start() [Stop]() dTmp1 += System.Convert.ToDecimal(_StopTick) - System.Convert.ToDecimal(_StartTick) Next 'Get Average Overhead _TimerOverhead = dTmp1 / Iterations / TickFrequency 'Reset Average Accus AvgReset() End If End Sub End Class
|
|
|
| |
|
|
| |
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|