How to use the QueryPerformanceCounter function to time code in Visual C++ (815668)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
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:
IN THIS TASKINTRODUCTIONThis 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
topBuild 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.
In Visual C++ .NET 2003, follow these steps:- Under Project Types, click Visual
C++ Projects.
- Under
Templates, click
Console Application (.NET).
In Visual C++ 2005, follow these steps:- Under Project Types, click Visual
C++.
- Under
Templates, click
CLR Console Application.
- In the Name box, type
CodeTimer.
- In the Location box, type
C:\Test, and then click OK.
- In Solution Explorer, double-click the CodeTimer.cpp
file in the Source Files folder.
- Add the following code after the #include "stdafx.h" line:
#include <tchar.h>
#include <windows.h> - 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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- 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: - 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.
- Press ENTER to stop the application and to close
the Console window.
back to the topTroubleshootingYou 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
topREFERENCESFor additional information about the QueryPerformanceCounter function, visit the following Microsoft Developer Network (MSDN) Web site: For additional information about the QueryPerformanceFrequency function, visit the following MSDN Web site: For
additional information about the timeGetTime function, visit the
following MSDN Web
site: For
additional information about the GetTickCount function, visit the
following MSDN Web
site: back to the top
Modification Type: | Major | Last Reviewed: | 1/12/2006 |
---|
Keywords: | kbPerformance kbProgramming kbConsole kbcode kbHOWTOmaster KB815668 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|