zoukankan      html  css  js  c++  java
  • Qt测算程序运行时间

    #include <QDebug>  
    #include <QTime>  
    #include <sys/time.h>  
    #include <windows.h>  
    #include <math.h>  
      
    void function();  //测试函数
      
    int main(void)  
    {  
        qDebug()<<"-------------------------------"; 
        /*************************************************/
        //方法1 利用QTime,其精度为ms级
        QTime time;  
        time.start();  
        function();  
        qDebug()<<time.elapsed()/1000.0<<"s"; 
        //运行结果0.109s
        
        /*************************************************/
        //方法2 利用gettimeofday(),其精度为us级  
        struct timeval tpstart,tpend;  
        float timeuse;  
        gettimeofday(&tpstart,NULL);  
        function();  
        gettimeofday(&tpend,NULL);  
        timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000000.0;  
        qDebug()<<timeuse<<"s";  
        //运行结果:0.109375 s
      
        /*************************************************/
        //方法3 利用clock(),其精度为ms级  
        double time_Start = (double)clock();  
        function();  
        double time_End = (double)clock();  
        qDebug()<<(time_End - time_Start)/1000.0<<"s"; 
        //运行结果:0.11 s
      
        /*************************************************/
        //方法4 利用windows.h(VC)函数,提精度为us级  
        LARGE_INTEGER litmp;  
        LONGLONG Qpart1,Qpart2,Useingtime;  
        double dfMinus,dfFreq,dfTime;  
      
        //获得CPU计时器的时钟频率  
        QueryPerformanceFrequency(&litmp);//取得高精度运行计数器的频率f,单位是每秒多少次(n/s),  
        dfFreq = (double)litmp.QuadPart;  
      
        QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值  
        Qpart1 = litmp.QuadPart; //开始计时  
      
        function(); //待测试的计算函数等  
      
        QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值  
        Qpart2 = litmp.QuadPart; //终止计时  
      
        dfMinus = (double)(Qpart2 - Qpart1);//计算计数器值  
        dfTime = dfMinus / dfFreq;//获得对应时间,单位为秒,可以乘1000000精确到微秒级(us)  
        Useingtime = dfTime*1000000;  
      
        qDebug()<<dfTime<<"s";  
        //运行结果:0.107415 s
        /*************************************************/
        return 0;  
    }  
      
    //测试函数
    void function()  
    {  
        unsigned int i,j;  
        double y;  
      
        for(i=0;i<1000;i++)  
            for(j=0;j<1000;j++)  
                y=sin((double)i);  
    } 

    原文:https://blog.csdn.net/hebbely/article/details/78953318

  • 相关阅读:
    如何禁止用户直接对TextBox进行数据粘贴?(ASP.NET WEB开发)
    jquery过滤选择器前加空格与不加空格的区别(转)
    linux设置ip.dns.gateway
    Adobe Fireworks CS4 序列号(注册码)
    AS3清空数组的四种方法
    Flash中用AS3做的游戏,导出apk安装到手机上滤镜效果出不来为什么?
    用AS3清空容器下所有子显示对象
    对Linux进程的理解
    C++基础
    虚拟机三种网络模式(hostonly、Bridged、NAT)
  • 原文地址:https://www.cnblogs.com/nanqiang/p/10576235.html
Copyright © 2011-2022 走看看