HOWTO: Use QueryPerformanceCounter to Time CodeLast reviewed: September 29, 1997Article ID: Q172338 |
The information in this article applies to:
SUMMARYWhen timing code to identify performance bottlenecks, you want to use the highest resolution timer the system has to offer. This article describes how to use the QueryPerformanceCounter function to time application code.
MORE INFORMATIONSeveral timers of differing accuracy are offered by the operating system:
Function Units Resolution Now, Time, Timer seconds 1 second GetTickCount milliseconds approx. 10 ms TimeGetTime milliseconds approx. 10 msQueryPerformanceCounter QueryPerformanceFrequency same If your system supports a high-resolution counter, you can use QueryPerformanceCounter and QueryPerformanceFrequency to do high-resolution timings. The following sample code compares the various counters: WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
Step-by-Step Procedures
On the other hand, QueryPerformanceCounter changes value between successive API calls, indicating its usefulness in high-resolution timing. The resolution in this case is on the order of a microsecond. Because the resolution is system-dependent, there are no standard units that it measures. You have to divide the difference by the QueryPerformanceFrequency to determine the number of seconds elapsed. In the case above, the overhead for just calling the API is about 19 microseconds. This would have to be subtracted when timing other code as follows:
Private Sub Time_Addition() Dim Ctr1 As Currency, Ctr2 As Currency, Freq As Currency Dim Overhead As Currency, A As Long, I As Long QueryPerformanceFrequency Freq QueryPerformanceCounter Ctr1 QueryPerformanceCounter Ctr2 Overhead = Ctr2 - Ctr1 ' determine API overhead QueryPerformanceCounter Ctr1 ' time loop For I = 1 To 100 A = A + I Next I QueryPerformanceCounter Ctr2 Debug.Print "("; Ctr1; "-"; Ctr2; "-"; Overhead; ") /"; Freq Debug.Print "100 additions took"; Debug.Print (Ctr2 - Ctr1 - Overhead) / Freq; "seconds" End SubSample output:
( 3630876.6256 - 3630876.6388 - 0.0013 ) / 119.3182 100 additions took 9.97333181358753E-05 secondsNOTE: Because currency variables are used, the values returned are 10000 times smaller than the actual counters. Because the calculation of seconds involves a division operation, this factor is cancelled out.
REFERENCESMicrosoft Developer Network; topics: timeGetTime GetTickCount QueryPerformanceCounter QueryPerformanceFrequency Keywords : APrgOther VB4ALL VB4WIN vb5all vb5howto VBKBWinAPI vbwin GnrlVb kbprg Technology : kbvba Version : WINDOWS:4.0 5.0 7.0 97 Platform : WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |