Windows 毫秒计时

#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;
}

猜你喜欢

转载自www.cnblogs.com/drfxiaoliuzi/p/8876579.html