• 【Qt开发】Qt测试计算时间


    方法1 利用QTime,其精度为ms级
    
    </pre><pre code_snippet_id="1852215" snippet_file_name="blog_20160826_3_9895116" name="code" class="cpp">#include <QDebug>
    #include <QTime>
    
    QTime time;
    
    time.start();
    function();
    
    qDebug()<<time.elapsed()/1000.0<<"s";
    
    
    方法2 利用gettimeofday(),其精度为us级


    #include <QDebug>
    #include <sys/time.h>
    
    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";
    

    方法3 利用clock(),其精度为ms级

    #include <QDebug>
    #include <sys/time.h>
    
    double time_Start = (double)clock();
    function();
    double time_End = (double)clock();
        
    qDebug()<<(time_End - time_Start)/1000.0<<"s";
    
    方法4 利用windows.h(VC)函数,提精度为us级




    #include <QDebug>
    #include <windows.h>
    
    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";
    


  • 相关阅读:
    数字配对(bzoj 4514)
    任务查询系统(bzoj 3932)
    楼房重建(bzoj 2957)
    Hotel(poj 3667)
    Can you answer these queries(spoj 1043)
    亚瑟王(bzoj 4008)
    潘多拉的盒子(bzoj 1194)
    Circling Round Treasures(codeforces 375c)
    莫队算法---基础知识介绍(转载)
    HDU 1141---Brackets Sequence(区间DP)
  • 原文地址:https://www.cnblogs.com/huty/p/8518254.html
Copyright © 2020-2023  润新知