High-precision timing with the QueryPerformanceCounter API function

High-precision timing with the QueryPerformanceCounter API function

While VB Timer functions is sufficiently precise for most tasks, it doesn’t provide the highest possible resolution most modern computer can provide. If your hardware supports high-resolution counters, you can do a much better job with a pair of API functions:

Private Type LARGE_INTEGER    lowpart As Long    highpart As LongEnd TypePrivate Declare Function QueryPerformanceFrequency Lib "kernel32" Alias _    "QueryPerformanceFrequency" (lpFrequency As LARGE_INTEGER) As LongPrivate Declare Function QueryPerformanceCounter Lib "kernel32" Alias _    "QueryPerformanceCounter" (lpPerformanceCount As LARGE_INTEGER) As Long

You should call QueryPerformanceFrequency once, at the beginning of your program, to retrieve the internal frequency of the high-res timer, that is the number of ticks it provides each second. Then you can call QueryPerformanceCounter as many times as you wish: each call returns the value of the internal counter. For example, you retrieve the value of the counter at the beginning and the end of a piece of code you want to time, and if you subtract the former from the latter value you get the number of ticks elapsed in the meantime. Converting this value into seconds only requires that you divide it by the frequency found previously.

The problem with these APIs is that VB doesn’t support large integers, that is 64-bit integers. This means that you are compelled to do process the high and low halves of those large numbers separatedly, which amounts to a lot of code and overhead.

Fortunately there is a neat trick that solves this problem quite nicely. Instead of using LARGE_INTEGERS, you can use Currency variables. After all, Currency values are plain 64-bit integers that VB scales by a factor of 10,000 any time you assign a value to them and the read it back. To use Currency variables instead of LARGE_INTEGER values you must use aliased functions:

Private Declare Function QueryPerformanceFrequencyAny Lib "kernel32" Alias _    "QueryPerformanceFrequency" (lpFrequency As Any) As LongPrivate Declare Function QueryPerformanceCounterAny Lib "kernel32" Alias _    "QueryPerformanceCounter" (lpPerformanceCount As Any) As Long

Here’s a piece of code that illustrates how to use these two API functions:

Private Sub Command1_Click()    Dim frequency As Currency    Dim startTime As Currency    Dim endTime As Currency    Dim result As Double        ' get the frequency counter    ' return zero if hardware doesn't support high-res performance counters    If QueryPerformanceFrequencyAny(frequency) = 0 Then         MsgBox "This computer doesn't support high-res timers", vbCritical        Exit Sub    End If        ' start timing    QueryPerformanceCounterAny startTime        ' put here the code to be timed, for example...    Dim i As Long    For i = 1 To 1000000    Next    ' end timing        QueryPerformanceCounterAny endTime        ' note that both dividend and divisor are scaled    ' by 10,000, so you don't need to scale the result    result = (endTime - startTime) / frequency        ' show the result    MsgBox resultEnd Sub

As you see, in this particular case you don’t even need to take the 10,000 scaling factor into account, because both operands of the division near the end of the procedure are scaled by the same factor, so you can (and must) ignore it.

See also  Who are the Modern GPU Cloud Providers?

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist