zoukankan      html  css  js  c++  java
  • C/C++/Qt 统计运行时间

    http://www.cnblogs.com/Romi/archive/2012/04/19/2457175.html

    程序中经常需要统计时间,需要统计某项运算的运行时间时,需要计算时间差。

    1. C/C++

    C中有基础库用于实现该功能,功能在time.h头文件中,代码实例如下:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void main()
    {
        double time_Start = (double)clock(); //开始时间
        //操作。。。
        double timr_Finish = (double)clock(); //结束时间
    
        printf("operate time: %.2fms",(time_Finish-time_Start); //输出
    }
    

    2. QT

    Qt程序中也可以使用C的方法,当然Qt内部封装了一个时间统计的方法:QTime类(注意不是QTimer,QTimer是计时用的)

    QTime类使用手册将官方文档:http://qt-project.org/doc/qt-4.8/qtime.html

    代码示例如下:

    #include <QTime>
    
    QTime time;
    time.start(); //开始计时,以ms为单位
    int time_Diff = time.elapsed(); //返回从上次start()或restart()开始以来的时间差,单位ms
    
    //以下方法是将ms转为s
    float f = time_Diff/1000.0;
    QString tr_timeDiff = QString("%1").arg(f); //float->QString
    

     其他的很多库都会对时间统计方法进行封装,对于统计运行时间差余以为调用C的方法是最好用的

  • 相关阅读:
    topcoder srm 681 div1
    topcoder srm 683 div1
    topcoder srm 684 div1
    topcoder srm 715 div1
    topcoder srm 685 div1
    topcoder srm 687 div1
    topcoder srm 688 div1
    topcoder srm 689 div1
    topcoder srm 686 div1
    topcoder srm 690 div1 -3
  • 原文地址:https://www.cnblogs.com/mmix2009/p/3590556.html
Copyright © 2011-2022 走看看