Click to Rate and Give Feedback
MSDN
MSDN Library
User Interface
Windowing
Timers
Timer Reference
Functions
 QueryPerformanceCounter Function
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

Community Content   What is Community Content?
Add new content RSS  Annotations
vb.net syntax      dmex   |   Edit   |   Show History
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function QueryPerformanceCounter(<Out> ByRef lpPerformanceCount As Long) As Boolean
End Function
Tags What's this?: Add a tag
Flag as ContentBug
C# syntax      dmex   |   Edit   |   Show History
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
Tags What's this?: Add a tag
Flag as ContentBug
C++ class HRTimer. Member functions using QueryPerformanceCounter/Frequency on multicore machine.      MatiasFG   |   Edit   |   Show History
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);
}
broken link      dfields ... Thomas Lee   |   Edit   |   Show History
The link to SetThreadAffinityMask is broken; it should point to:
http://msdn.microsoft.com/en-us/library/ms686247(VS.85).aspx
StopTimer function should return elapsed time in seconds      peter_ne   |   Edit   |   Show History
It makes more sense to divide by frequency in the StopTimer function, so it returns the elapsed time in seconds:
return ((stop.QuadPart - start.QuadPart) / frequency);
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker