Microsoft KB Archive/815668

= How to use the QueryPerformanceCounter function to time code in Visual C++ =

Article ID: 815668

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-







For a Microsoft Visual C# .NET version of this article, see 306979.



For a Microsoft Visual Basic .NET version of this article, see 306978.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System

IN THIS TASK

 * INTRODUCTION
 * Build the demonstration application, and then run the demonstration application
 * Troubleshooting
 * REFERENCES



INTRODUCTION
This step-by-step article describes how to use the QueryPerformanceCounter function to time your application code. When you time your application code to identify performance bottlenecks, you want to use the highest resolution timer that the system has to offer.

back to the top

Build the demonstration application, and then run the demonstration application
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. In Microsoft Visual C++ .NET 2002, follow these steps:  Under Project Types, click Visual C++ Projects. Under Templates, click Managed C++ Application.</ol>

In Visual C++ .NET 2003, follow these steps: <ol style="list-style-type: lower-alpha;"> Under Project Types, click Visual C++ Projects.</li> Under Templates, click Console Application (.NET).</li></ol>

In Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> Under Project Types, click Visual C++.</li> Under Templates, click CLR Console Application.</li></ol> </li> In the Name box, type CodeTimer .</li> In the Location box, type C:\Test, and then click OK.</li> In Solution Explorer, double-click the CodeTimer.cpp file in the Source Files folder.</li>  Add the following code after the #include &quot;stdafx.h&quot; line:  Replace the code in the _tmain function with the following code: __int64 ctr1 = 0, ctr2 = 0, freq = 0; int acc = 0, i = 0;
 * 1) include <tchar.h>
 * 2) include <windows.h> </li>

// Start timing the code. if (QueryPerformanceCounter((LARGE_INTEGER *)&ctr1)!= 0) {   // Code segment is being timed. for (i=0; i<100; i++) acc++;

// Finish timing the code. QueryPerformanceCounter((LARGE_INTEGER *)&ctr2);

Console::WriteLine(&quot;Start Value: {0}&quot;,ctr1.ToString); Console::WriteLine(&quot;End Value: {0}&quot;,ctr2.ToString); QueryPerformanceFrequency((LARGE_INTEGER *)&freq); Console::WriteLine(S&quot;QueryPerformanceCounter minimum resolution: 1/{0} Seconds.&quot;,freq.ToString); // In Visual Studio 2005, this line should be changed to: Console::WriteLine(&quot;QueryPerformanceCounter minimum resolution: 1/{0} Seconds.&quot;,freq.ToString); Console::WriteLine(&quot;100 Increment time: {0} seconds.&quot;,((ctr2 - ctr1) * 1.0 / freq).ToString); } else {   DWORD dwError = GetLastError; Console::WriteLine(S&quot;Error value = {0}&quot;,dwError.ToString);// In Visual Studio 2005, this line should be changed to: Console::WriteLine(&quot;Error value = {0}&quot;,dwError.ToString); }

// Make the console window wait. Console::WriteLine; Console::Write(&quot;Press ENTER to finish.&quot;); Console::Read;

return 0; Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li>  Save the application. Press CTRL+F5 to compile and to run the application.

The Console window displays output that is similar to the following code sample: Start Value: 192244814693193 End Value: 192244814694677 QueryPerformanceCounter minimum resolution: 1/1263460000 Seconds. 100 Increment time: 1.17455241954633E-06 seconds.

Press ENTER to finish. </li> Press ENTER to stop the application and to close the Console window.</li></ol>

back to the top

Troubleshooting
You can use the following ways to help you resolve problems when you are troubleshooting your application code:
 * The API call may fail under some circumstances. Check the return value, and then adjust your application code to make sure that you receive valid results.
 * For the best results, test the application code multiple times. When you test the application code, make sure no other applications and no other server processes are running. Activities in other threads and in other processes can affect the percentage of time that the system spends in the target application.

back to the top

<div class="references_section">