'Use this code to create a class called HiResTimer:
'The number is codified as HighPart*2^32+LowPart
Private Type LARGE_INTEGER
LowPart As Long
HighPart As Long
End Type
Private Declare Function QueryPerformanceCounter Lib _
"kernel32" (lpPerformanceCount As LARGE_INTEGER) _
As Long
Private Declare Function QueryPerformanceFrequency Lib _
"kernel32" (lpFrequency As LARGE_INTEGER) As Long
Private m_TicksPerSecond As Double
Private m_LI0 As LARGE_INTEGER
Private m_LI1 As LARGE_INTEGER
Friend Sub Class_Initialize()
Dim LI As LARGE_INTEGER
If QueryPerformanceFrequency(LI) <> 0 Then
m_TicksPerSecond = LI2Double(LI)
Else
m_TicksPerSecond = -1
End If
End Sub
Friend Property Get Resolution() As Double
Resolution = 1# / m_TicksPerSecond
End Property
Friend Sub EnterBlock()
QueryPerformanceCounter m_LI0
End Sub
Friend Sub ExitBlock()
QueryPerformanceCounter m_LI1
End Sub
Friend Property Get ElapsedTime() As Double
Dim EnterTime As Double, ExitTime As Double
MsgBox "Elapsed Time [usecs]:" & hrt.ElapsedTime * _
1000000#
Believe it or not, you can time even native-compiled code division. For more information, look at the MSDN Library description of the kernel APIs used here. On x86 architectures, resolution is better that 1 microsecond. Be careful, however, of trusting single instance timings, as you’ll find the “resolution” of this performance counter varies over time. In fact, the overhead of simply calling QueryPerformanceCounter in VB is quite a measurable time period itself.
Although you can time single operations, you’re still better off averaging the time required for hundreds or thousands of similar operations.
—Alessandro Coppo, Rapallo, Italy
---------------------------------------------------------
From VBPJ