工具---程序中的耗时问题

 计算时间的函数

  在实际开发中,尤其做算法的,有时为了比较不同的算法或者优化算法,需要计算各个算法运行的时间或者关键代码段的运行时间,以此来衡量算法在速度上的优劣或者进行代码优化时的一个参考。因此,如何学会计算程序的运行时间是一个基本的问题。

  这里搜罗了部分时间函数,不限于这些:

--------------------python---------------------  
#方法1
     
    import datetime    
    starttime = datetime.datetime.now()   
    #long running     
    endtime = datetime.datetime.now()    
    print (endtime - starttime).seconds
     
 #方法 2    
    start = time.time()    
    run_fun()    
    end = time.time()    
    print end-start
     
#方法3    
    start = time.clock()    
    run_fun()    
    end = time.clock()    
    print end-start
     
#方法1和方法2都包含了其他程序使用CPU的时间,是程序开始到程序结束的运行时间。   
#方法3算只计算了程序运行的CPU时间
-----------------------------opencv-----------------
//注意添加头文件
#include  <cv.h>
using namespace std;
 
double t = (double)cvGetTickCount();
 
//。。。。。。算法或程序代码
 
t = (double)cvGetTickCount() - t;
printf( "processing time = %gms
", t/(cvGetTickFrequency()*1000) );//输出时间为ms
printf( "processing time = %gs
", t/(cvGetTickFrequency()*1000000) );//输出时间为s

-------------------------------------------------------
OpenCV 利用getTickCount()与getTickFrequency()计算执行时间
double t1 = (double)getTickCount();
.
.
.
double t2 = (double)getTickCount();
cout<<"time:"<<(t2-t1)*1000/(getTickFrequency())<<endl;
函数解释:
getTickCount()函数:它返回从操作系统启动到当前所经的计时周期数。
getTickFrequency()函数:返回CPU的频率。// getTickFrequency 获取CPU一秒钟走过的时钟周期数
--------------------------------------------------------------
在OpenCV编程中,可能会遇到比较不同算法之间的运算复杂度及时耗的问题,下面给出一个统计代码运行时间的demo,里面用到getTickCount函数,使用时需要添加头文件#include "opencv2/imgproc/imgproc.hpp"。
#include<iostream>
#include <opencv2/opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
 
using namespace cv;
using namespace std;
 
int main(){
    double t = (double)getTickCount();//开始时间
    for (int i = 0; i <= 1000; i++){}//可替换成其他代码
    t = (double)getTickCount() - t;//代码运行时间=结束时间-开始时间
    printf("execution time = %gms
", t*1000. / getTickFrequency());//转换时间单位并输出代码运行时间
    system("pause");//让dos界面停留
    return 0;
}
-------------------------------------------------
使用方法:
double t = (double)getTickCount();

// do something ...

t = ((double)getTickCount() - t)/getTickFrequency();

所用函数为getTickCount()和getTickFrequency()。

getTickCount():返回CPU自某个时间(如启动电脑)以来走过的时钟周期数。

getTickFrequency():返回CPU一秒中所走的时钟周期数。所以可以以秒为单位对某运算时间计时。

使用方法:

    double start = static_cast<double>(getTickCount());
    double time = ((double)getTickCount() - start) / getTickFrequency();
    cout << "run time: " << time << "s" << endl;

也可用函数cvGetTickCount()和cvGetTickFrequency()。但注意,此时得到的单位是us级的统计时间。

    double start = static_cast<double>(cvGetTickCount());
    double time = ((double)cvGetTickCount() - start) / cvGetTickFrequency();
    cout << "run time: " << time << "s" << endl;
-----------------------------C++---------------------
c/c++ 计算程序运行时间,精确到毫秒
#include <time.h>

int main()

{
    clock_t start,end;
    start=clock();

//To do

    end=clock();
    printf("totile time=%f(ms)
",(float)(end-start)*1000/CLOCKS_PER_SEC);

}
原文地址:https://www.cnblogs.com/carle-09/p/11325530.html