QueryPerformanceCounter
BOOL QueryPerformanceCounter(
LARGE_INTEGER *lpPerformanceCount
);
Description
Retrieves the current value of the high-resolution performance counter.
The performance counter is a time-of-day counter. The current value of the counter is retrieved in the lpPerformanceCount
parameter.
To convert the counter value to a meaningful unit of time, you need to use the QueryPerformanceFrequency
function to get the frequency of the counter. The frequency is the number of ticks per second. Divide the counter value by the frequency to obtain the duration in seconds.
Parameters
Name | Type | Description |
---|---|---|
lpPerformanceCount |
PLARGE_INTEGER ( LARGE_INTEGER* ) |
A pointer to a LARGE_INTEGER that receives the current performance-counter value. |
Return Value
Type | Description |
---|---|
BOOL |
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError . |
Remarks
Requirement | Description |
---|---|
Minimum supported client | Windows Vista [desktop apps only] |
Minimum supported server | Windows Server 2008 [desktop apps only] |
Target Platform | Windows |
Header | Windows.h |
Library | Corelt.lib |
DLL | Corelt.dll |
Example
#include <windows.h>
#include <iostream>
int main() {
LARGE_INTEGER frequency;
LARGE_INTEGER startCount;
LARGE_INTEGER endCount;
double elapsedTime = 0.0;
// Get the performance counter frequency
if (QueryPerformanceFrequency(&frequency)) {
// Get the starting performance counter value
if (QueryPerformanceCounter(&startCount)) {
// Simulate some work
Sleep(100); // Sleep for 100 milliseconds
// Get the ending performance counter value
if (QueryPerformanceCounter(&endCount)) {
// Calculate the elapsed time in seconds
elapsedTime = static_cast<double>(endCount.QuadPart - startCount.QuadPart) / frequency.QuadPart;
std::cout << "Elapsed time: " << elapsedTime << " seconds." << std::endl;
}
}
} else {
std::cerr << "QueryPerformanceFrequency failed. Error: " << GetLastError() << std::endl;
}
return 0;
}