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

Article translations Article translations
Article ID: 815668 - View products that this article applies to.
Expand all | Collapse all

On This Page

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.



Build the demonstration application, and then run the demonstration application

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. In Microsoft Visual C++ .NET 2002, follow these steps:
    1. Under Project Types, click Visual C++ Projects.
    2. Under Templates, click Managed C++ Application.
    In Visual C++ .NET 2003, follow these steps:
    1. Under Project Types, click Visual C++ Projects.
    2. Under Templates, click Console Application (.NET).
    In Visual C++ 2005, follow these steps:
    1. Under Project Types, click Visual C++.
    2. Under Templates, click CLR Console Application.
  4. In the Name box, type CodeTimer.
  5. In the Location box, type C:\Test, and then click OK.
  6. In Solution Explorer, double-click the CodeTimer.cpp file in the Source Files folder.
  7. Add the following code after the #include "stdafx.h" line:
    #include <tchar.h>
    #include <windows.h>
  8. Replace the code in the _tmain function with the following code:
    __int64 ctr1 = 0, ctr2 = 0, freq = 0;
    int acc = 0, i = 0;
    
    // 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("Start Value: {0}",ctr1.ToString());
    	Console::WriteLine("End Value: {0}",ctr2.ToString());
    	
    	QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
    	
    	Console::WriteLine(S"QueryPerformanceCounter minimum resolution: 1/{0} Seconds.",freq.ToString());
    // In Visual Studio 2005, this line should be changed to: Console::WriteLine("QueryPerformanceCounter minimum resolution: 1/{0} Seconds.",freq.ToString());	
    Console::WriteLine("100 Increment time: {0} seconds.",((ctr2 - ctr1) * 1.0 / freq).ToString());
    }
    else
    {
    	DWORD dwError = GetLastError();
    	Console::WriteLine(S"Error value = {0}",dwError.ToString());// In Visual Studio 2005, this line should be changed to: Console::WriteLine("Error value = {0}",dwError.ToString());
    }
    
    // Make the console window wait.
    Console::WriteLine();
    Console::Write("Press ENTER to finish.");
    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:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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.
    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
  9. 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.
    
    
  10. Press ENTER to stop the application and to close the Console window.

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.

REFERENCES

For additional information about the QueryPerformanceCounter function, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/library/ms886788.aspx
For additional information about the QueryPerformanceFrequency function, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/ms886789.aspx
For additional information about the timeGetTime function, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/ms713418.aspx
For additional information about the GetTickCount function, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/ms724408.aspx

Properties

Article ID: 815668 - Last Review: November 14, 2007 - Revision: 3.2
APPLIES TO
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
Keywords: 
kbperformance kbprogramming kbconsole kbcode kbhowtomaster KB815668

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com