|
#include <stdio.h>
#include <windows.h>
double PCFreq = 0.0;
__int64 CounterStart = 0;
void StartCounter()
{
LARGE_INTEGER li;
if(!QueryPerformanceFrequency(&li))
printf( "QueryPerformanceFrequency failed!\n");
printf("%d\n",li.QuadPart);
PCFreq = double(li.QuadPart)/1000000000.0;
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
}
double GetCounter()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return double(li.QuadPart-CounterStart)/PCFreq;
}
int main()
{
StartCounter();
Sleep(1);
printf("%f\n",GetCounter());
getchar();
return 0;
}
以上程序可以实现纳秒级的计时。主要用到QueryPerformanceFrequency和QueryPerformanceCounter函数。