Windows系统精准计时

#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

猜你喜欢

转载自blog.csdn.net/u013445609/article/details/80097314