<Linux+Qt>计时函数

【问题描述】程序运行时间是编程的一项重要指标,如何测算程序的运行时间呢?

【解析】

测试函数

[html]  view plain copy
  1. #include <math.h>  
  2.   
  3. void function()  
  4. {  
  5.     unsigned int i,j;  
  6.     double y;  
  7.   
  8.     for(i=0;i<1000;i++)  
  9.         for(j=0;j<1000;j++)  
  10.             y=sin((double)i);  
  11. }  

 

方法1 如果在QT中,利用QTime,其精度为ms级----------在QT中使用。

[html]  view plain copy
  1. #include <QDebug>  
  2. #include <QTime>  
  3.   
  4. QTime time;  
  5.   
  6. time.start();  
  7. function();  
  8.   
  9. qDebug()<<time.elapsed()/1000.0<<"s";  

运行结果:0.109 s

方法2 利用gettimeofday(),其精度为us级----------C语言实现,在C++环境、QT、VC中也能用。

[html]  view plain copy
  1. #include <QDebug>  
  2. #include <sys/time.h>  
  3.   
  4. struct timeval tpstart,tpend;  
  5. float timeuse;  
  6.   
  7. gettimeofday(&tpstart,NULL);  
  8. function();  
  9. gettimeofday(&tpend,NULL);  
  10. timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000000.0;  
  11.   
  12. qDebug()<<timeuse<<"s";  

运行结果:0.109375 s


方法3 利用clock(),其精度为ms级----------C++实现,在VC和QT中也可以使用。

 
[html]  view plain copy
  1. #include <QDebug>  
  2. #include <sys/time.h>  
  3.   
  4. double time_Start = (double)clock();  
  5. function();  
  6. double time_End = (double)clock();  
  7.       
  8. qDebug()<<(time_End - time_Start)/1000.0<<"s";  

运行结果:0.11 s

方法4 利用windows.h(VC)函数,其精度为us级----------在VC和QT中使用。

[html]  view plain copy
  1. #include <QDebug>  
  2. #include <windows.h>  
  3.   
  4. LARGE_INTEGER litmp;  
  5. LONGLONG Qpart1,Qpart2,Useingtime;  
  6. double dfMinus,dfFreq,dfTime;  
  7.   
  8. //获得CPU计时器的时钟频率  
  9. QueryPerformanceFrequency(&litmp);//取得高精度运行计数器的频率f,单位是每秒多少次(n/s),  
  10. dfFreq = (double)litmp.QuadPart;  
  11.   
  12. QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值  
  13. Qpart1 = litmp.QuadPart; //开始计时  
  14.   
  15. function(); //待测试的计算函数等  
  16.   
  17. QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值  
  18. Qpart2 = litmp.QuadPart; //终止计时  
  19.   
  20. dfMinus = (double)(Qpart2 - Qpart1);//计算计数器值  
  21. dfTime = dfMinus / dfFreq;//获得对应时间,单位为秒,可以乘1000000精确到微秒级(us)  
  22. Useingtime = dfTime*1000000;  
  23.   
  24. qDebug()<<dfTime<<"s";  

运行结果:0.107415 s

 

【代码清单】 

[html]  view plain copy
  1. #include <QDebug>  
  2. #include <QTime>  
  3. #include <sys/time.h>  
  4. #include <windows.h>  
  5. #include <math.h>  
  6.   
  7. void function();  
  8.   
  9. int main(void)  
  10. {  
  11.     qDebug()<<"-------------------------------";  
  12.     //-1-  
  13.     QTime time;  
  14.     time.start();  
  15.     function();  
  16.     qDebug()<<time.elapsed()/1000.0<<"s";  
  17.   
  18.     //-2-  
  19.     struct timeval tpstart,tpend;  
  20.     float timeuse;  
  21.     gettimeofday(&tpstart,NULL);  
  22.     function();  
  23.     gettimeofday(&tpend,NULL);  
  24.     timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000000.0;  
  25.     qDebug()<<timeuse<<"s";  
  26.   
  27.     //-3-  
  28.     double time_Start = (double)clock();  
  29.     function();  
  30.     double time_End = (double)clock();  
  31.     qDebug()<<(time_End - time_Start)/1000.0<<"s";  
  32.   
  33.     //-4-  
  34.     LARGE_INTEGER litmp;  
  35.     LONGLONG Qpart1,Qpart2,Useingtime;  
  36.     double dfMinus,dfFreq,dfTime;  
  37.   
  38.     //获得CPU计时器的时钟频率  
  39.     QueryPerformanceFrequency(&litmp);//取得高精度运行计数器的频率f,单位是每秒多少次(n/s),  
  40.     dfFreq = (double)litmp.QuadPart;  
  41.   
  42.     QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值  
  43.     Qpart1 = litmp.QuadPart; //开始计时  
  44.   
  45.     function(); //待测试的计算函数等  
  46.   
  47.     QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值  
  48.     Qpart2 = litmp.QuadPart; //终止计时  
  49.   
  50.     dfMinus = (double)(Qpart2 - Qpart1);//计算计数器值  
  51.     dfTime = dfMinus / dfFreq;//获得对应时间,单位为秒,可以乘1000000精确到微秒级(us)  
  52.     Useingtime = dfTime*1000000;  
  53.   
  54.     qDebug()<<dfTime<<"s";  
  55.   
  56.     return 0;  
  57. }  
  58.   
  59. void function()  
  60. {  
  61.     unsigned int i,j;  
  62.     double y;  
  63.   
  64.     for(i=0;i<1000;i++)  
  65.         for(j=0;j<1000;j++)  
  66.             y=sin((double)i);  
  67. }  
  1. ----------------------------------------------------------------------------
  2. 另外,方法2可具体参考:
  3. 在测试程序时,我们往往需要了解程序执行所需的时间,在C语言可以使用函数gettimeofday来得到时间,它的调用格式是:

    #include <sys/time.h>int gettimeofday(struct timeval *tv,struct timezone *tz);int settimeofday(const struct timeval *tv , const structtimezone *tz);

    其中结构timeval的定义为:

    strut timeval {long tv_sec;long tv_usec;};

    可以看出,使用这种方式计时,精度可达微秒。

    进行计时的时候,我们需要前后调用两次gettimeofday,然后计算中间的差值,实例代码如下:

    ************************************

    gettimeofday( &start, NULL );

    foo(); //示例函数

    gettimeofday( &end, NULL);

    timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec -start.tv_usec;

    timeuse /=1000000;

    ***********************************

    其中start,end 是结构体所定义的timeval型。

  4. --------------------------------------------------------------------------------
  5. 方法3可具体参考:
  6. 我现在用C++语言写了一段程序,想计算这段程序运行的准确时间,这是要用于跟其它实验结果作对比的,所以要精确到毫秒,C++程序运行时间 确实很难掌握啊!

    C++程序运行时间中的计时函数是clock(),而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:  

        
        
    1. #ifndef   _CLOCK_T_DEFINED     
    2.  typedef   long   clock_t;     
    3.  #define   _CLOCK_T_DEFINED     
    4.  #endif    

    这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock   tick)数,在MSDN中称之为挂钟时间(wal-clock)。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对 它的定义:  

        
        
    1. #ifndef   _CLOCK_T_DEFINED     
    2.  typedef   long   clock_t;     
    3.  #define   _CLOCK_T_DEFINED     
    4.  #endif    

    很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:   

        
        
    1.  
    2. int   main(   void   )     
    3. {     
    4.       long         i   =   10000000L;     
    5.       clock_t   start,   finish;     
    6.       double     duration;     
    7.       /*   测量一个事件持续的时间*/     
    8.       printf(   "Time   to   do   %ld   empty   loops   is   ",   i   );     
    9.       start   =   clock();     
    10.       while(   i--   )             ;     
    11.       finish   =   clock();     
    12.       duration   =   (double)(finish   -   start)   /   CLOCKS_PER_SEC;     
    13.       printf(   "%f   seconds"n",   duration   );     
    14.       system("pause");     
    15. }    

    可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的C++程序运行时间 :

        
        
    1. void   elapsed_time()     
    2.   {     
    3.   printf("Elapsed   time:%u   secs."n",clock()/CLOCKS_PER_SEC);     
    4.   }    

    当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:  

        
        
    1.  
    2. int   main(   void   )     
    3. {     
    4.       long         i   =   10000000L;     
    5.       clock_t   start,   finish;     
    6.       double     duration;     
    7.       /*   测量一个事件持续的时间*/     
    8.       printf(   "Time   to   do   %ld   empty   loops   is   ",   i   );     
    9.       start   =   clock();     
    10.       while(   i--   )             ;     
    11.       finish   =   clock();     
    12.       duration   =   (double)(finish   -   start)   /   CLOCKS_PER_SEC;     
    13.       printf(   "%f   seconds"n",   duration   );     
    14.       system("pause");     
    15. }    

    上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一 些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C++程序运行时间 中,最小的计时单位是一毫秒。 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值