Windows 毫秒计时

#include <windows.h>
#include <iostream>
using namespace std;

LARGE_INTEGER MilliSecondTimeStamp()
{
	LARGE_INTEGER m_liPerfStart = { 0 };
	QueryPerformanceCounter(&m_liPerfStart);
	return m_liPerfStart;
}
long long MilliSecondTimeCost(LARGE_INTEGER begin, LARGE_INTEGER end)
{
	LARGE_INTEGER m_liPerfFreq = { 0 };
	//获取每秒多少CPU Performance Tick
	QueryPerformanceFrequency(&m_liPerfFreq);
	return ((end.QuadPart - begin.QuadPart) * 1000) / m_liPerfFreq.QuadPart;
}
int main(void)
{


	LARGE_INTEGER begin = MilliSecondTimeStamp();
	for (int i = 0; i < 10000; i++)
		cout << i << endl;
	LARGE_INTEGER end = MilliSecondTimeStamp();
	long long time = MilliSecondTimeCost(begin, end);
	cout << endl << "execute cost " << time << "ms" << endl;
	//int time=( ((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000)/m_liPerfFreq.QuadPart);
	//char buffer[100];
	//sprintf(buffer, "execute cost%d millisecond ", time);
	//cout << buffer << endl;
	return 0;
}
posted @ 2018-04-18 17:54  drfxiaoliuzi  阅读(451)  评论(0编辑  收藏  举报