c++实现精确计时

//获取比較准确是程序执行时间


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

int main(void)
{       


    system("color F0"); 


    cout.setf(ios::fixed);                     
    cout.setf(ios::showpoint);
    cout.precision(10);  


//获取时钟频率
    LARGE_INTEGER m_liPerfFreq={0};
    QueryPerformanceFrequency(&m_liPerfFreq); 

//获取初始计数
    LARGE_INTEGER m_liPerfStart={0};
    QueryPerformanceCounter(&m_liPerfStart);

//測试代码的位置
    for(int ix=0;ix<100000;ix++){
         cout<<" ";
    }

//获取最后计数
    LARGE_INTEGER liPerfNow={0};
    QueryPerformanceCounter(&liPerfNow);

//计算时间

    long long time=( ((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000)/m_liPerfFreq.QuadPart);


    cout<<time<<endl;





    system("pause");
    return 0;
}



原文地址:https://www.cnblogs.com/blfshiye/p/5132312.html