计时代码

本文介绍两种在C++中实现高精度计时的方法,一种是利用Windows API的QueryPerformanceCounter和QueryPerformanceFrequency函数来测量函数运行的微妙级时间,另一种是使用标准库中的clock函数来获取函数执行的时间。

// get micro second

   LARGE_INTEGER t1,t2,t3;

   QueryPerformanceFrequency(&t3);

   QueryPerformanceCounter(&t1);

 

// Do something

 

   QueryPerformanceCounter(&t2);

   LONGLONG quadtime = t2.QuadPart-t1.QuadPart;

   DWORD swTime = quadtime * 1000000/t3.QuadPart;

swTime即为获取的Do something的微妙时间。

 

       #include<ctime>

       //*************************************************************************//
        clock_t firsttime,secondtime;
        firsttime = clock();
        //*************************************************************************//
       //此处填写需要执行的函数
        //*************************************************************************//
        secondtime = clock();
        double timetotal1 = (double)(secondtime-firsttime)/CLOCKS_PER_SEC;
        double timetotal2 = (double)(secondtime-firsttime);
        //cout<<"该函数执行时间:"<<timetotal1<<"秒"<<endl;
        //cout<<"该函数执行时间:"<<timetotal2<<"毫秒"<<endl;
        //*************************************************************************//

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值