欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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
相关标签: Windows计时