23 out of 60 rated this helpful Rate this topic

QueryPerformanceCounter function

Retrieves the current value of the high-resolution performance counter.

Syntax

BOOL WINAPI QueryPerformanceCounter(
  __out  LARGE_INTEGER *lpPerformanceCount
);

Parameters

lpPerformanceCount [out]

Type: LARGE_INTEGER*

A pointer to a variable that receives the current performance-counter value, in counts.

Return value

Type: 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

On a multiprocessor computer, it should not matter which processor is called. However, you can get different results on different processors due to bugs in the basic input/output system (BIOS) or the hardware abstraction layer (HAL). To specify processor affinity for a thread, use the SetThreadAffinityMask function.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winbase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

Reference
QueryPerformanceFrequency
Conceptual
Timers

 

 

Send comments about this topic to Microsoft

Build date: 9/7/2011

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
StopTimer function can return elapsed time in seconds
It makes more sense to divide by frequency in the StopTimer function, so it returns the elapsed time (in seconds in this case):
return ((stop.QuadPart - start.QuadPart) / frequency);

Please remove obscureness.
Does the count represent time elapsed since the system boots up?
Does the count include time the system spends in sleep or hibernation?
Does the count have a drift with the system time returned GetSystemTimeAsFileTime() even without updating the system time?
    => Yes, refer to http://msdn.microsoft.com/en-us/magazine/cc163996.aspx
C++ class HRTimer. Member functions using QueryPerformanceCounter/Frequency on multicore machine.
class HRTimer {
public:
HRTimer(void)
double getFrequency(void);

void startTimer(void) ;

double stopTimer(void);
private:
LARGE_INTEGER start;

LARGE_INTEGER stop;

double frequency;

//..
}

HRTimer::HRTimer(void)
: start(),
stop(),
frequency()
{
frequency = this->GetFrequency();
}

double HRTimer::GetFrequency(void)
{
LARGE_INTEGER proc_freq;

if (!::QueryPerformanceFrequency(&proc_freq)) throw Exception(TEXT("QueryPerformanceFrequency() failed"));

return proc_freq.QuadPart;
}

void HRTimer::StartTimer(void)
{
DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);

::QueryPerformanceCounter(&start);

::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
}

double HRTimer::StopTimer(void)
{
DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);

::QueryPerformanceCounter(&stop);

::SetThreadAffinityMask(::GetCurrentThread(), oldmask);

return ((stop.QuadPart - start.QuadPart) / frequency);

// Shouldn't you divide by frequency instead of multiply?
//
// Yes, ideed I was. Old was: frequency = 1.0 / GetFrequency()
//
// Still, I think directly dividing is clearer, so I changed it.
}
SetThreadAffinityMask is wrong
0 is senseless.
VB6 Snippet
Declare Function QueryPerformanceFrequency Lib "Kernel32" (lpFrequency As Currency) As Long
Declare Function QueryPerformanceCounter Lib "Kernel32" (lpPerformanceCount As Currency) As Long

Dim mFrequency As Currency, mOverhead As Currency, mStart As Currency, mStop as Currency, mElapsed As Currency

QueryPerformanceFrequency mFrequency

'Calculate API overhead
Dim Ctr1 As Currency, Ctr2 As Currency
QueryPerformanceCounter Ctr1
QueryPerformanceCounter Ctr2
mOverhead = Ctr2 - Ctr1

QueryPerformanceCounter mStart

'Do your stuff here

QueryPerformanceCounter mStop

'Ellapsed time in milliseconds
mElapsed = ((mStop - mStart - mOverhead) / mFrequency) * 1000

Debug.Print Format$(mElapsed , "0.0000")
broken link
The link to SetThreadAffinityMask is broken; it should point to:
http://msdn.microsoft.com/en-us/library/ms686247(VS.85).aspx