©2009 Microsoft Corporation. All rights reserved.
QueryPerformanceCounter Function

The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter.

Syntax

BOOL QueryPerformanceCounter(      
    LARGE_INTEGER *lpPerformanceCount );

Parameters

lpPerformanceCount
[out] Pointer to a variable that receives the current performance-counter value, in counts.

Return Value

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 [ http://msdn.microsoft.com/en-us/library/cc428944(VS.85).aspx ] .

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 [ http://msdn.microsoft.com/en-us/library/cc429346(VS.85).aspx ] function.

Function Information

Minimum DLL Versionkernel32.dll
HeaderDeclared in Winbase.h, include Windows.h
Import libraryKernel32.lib
Minimum operating systems Windows 95, Windows NT 3.1
UnicodeImplemented as Unicode version.

See Also

Timers Overview [ http://msdn.microsoft.com/en-us/library/ms632592(VS.85).aspx ] , QueryPerformanceFrequency [ http://msdn.microsoft.com/en-us/library/ms644905(VS.85).aspx ]
Tags: performance 
 
Community Content
vb.net syntax   Last Edit 11:34 AM by dmex   
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function QueryPerformanceCounter(<Out> ByRef lpPerformanceCount As Long) As Boolean
End Function
Tags: 
C# syntax   Last Edit 11:35 AM by dmex   
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
Tags: 
C++ class HRTimer. Member functions using QueryPerformanceCounter/Frequency on multicore machine.   Last Edit 12:10 PM by MatiasFG   
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) : frequency(1.0 / 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);
}
Tags: c++ queryperformancecounter queryperformancefrequency setthreadaffinitymask