如何使用QueryPerformanceCounter在几秒钟内获得结果?

时间:2014-11-15 12:13:18

标签: c++ windows timer type-conversion

BOOL QueryPerformanceCounter(
__out LARGE_INTEGER *lpPerformanceCount
);

LARGE_IN
TEGER startTimer()
{
LARGE_INTEGER start;
DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0);
QueryPerformanceCounter(&start);
SetThreadAffinityMask(GetCurrentThread(), oldmask);
return
start;
}

LARGE_INTEGER endTimer()
{
LARGE_INTE
GER stop;
DWORD_PTR oldmask = SetThreadAffinityMask(GetCurrentThread(), 0);
QueryPerformanceCounter(&stop);
SetThreadAffinityMask(GetCurrentThread(), oldmask);
return
stop;
}

我正在使用这些功能,但我不确定它返回值的类型。 endTimer - startTimer =?如何将此结果转换为秒?

1 个答案:

答案 0 :(得分:1)

您需要获得频率并将它除以2个计数器差异。

LARGE_INTEGER fr,t1,t2;

QueryPerformanceCounter(&t1);

// some lengthy code ...

QueryPerformanceCounter(&t2);

QueryPerformanceFrequency(&fr);

double diff_sec = (t2.QuadPart-t1.QuadPart)/(double)fr.QuadPart;