Windows系统精准计时
程序员文章站
2024-01-23 18:04:28
...
#include <stdio.h>
#include "windows.h"
//windows系统精确计时
//BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);
//BOOL QueryPerformanceCounter (LARGE_INTEGER *lpCount);
void main() {
//cpu频率
LARGE_INTEGER nFreq;
//定时器当前计数值,开始
LARGE_INTEGER nBeginTime;
//定时器当前计数值,结束
LARGE_INTEGER nEndTime;
double time;
//返回值:非零,硬件支持高精度计数器;零,硬件不支持,读取失败
if (QueryPerformanceFrequency(&nFreq)) {
QueryPerformanceCounter(&nBeginTime);
//sleep有系统误差
Sleep(2000);
QueryPerformanceCounter(&nEndTime);
time = (double) (nEndTime.QuadPart - nBeginTime.QuadPart) / (double) nFreq.QuadPart;
printf("%f\n", time);
}
Sleep(1000);
system("Pause");
}
//运行结果:1.999581
下一篇: 测量程序运行时间