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.

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.

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

Tags : performance


Community Content

dmex
vb.net syntax
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function QueryPerformanceCounter(<Out> ByRef lpPerformanceCount As Long) As Boolean
End Function
Tags :

dmex
C# syntax
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
Tags :

MatiasFG
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) : 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);
}

Thomas Lee
broken link
The link to SetThreadAffinityMask is broken; it should point to:
http://msdn.microsoft.com/en-us/library/ms686247(VS.85).aspx
Tags : contentbug

Page view tracker