程序运行耗时计算

来源:互联网 发布:php redis 阻塞队列 编辑:程序博客网 时间:2024/05/17 08:18
程序运行耗时计算

常用的程序运行耗时计算有四中方法:

1,clock_t 头文件为time.h

clock_t start = clock();function();clock_t end = clock();std::cout<<"time is:"<<(double)(end -time)/CLOCKS_PER_SEC<<std::endl;

2,利用gettimeofday()

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,利用opencv中计时函数

int64 start = getTickCount();function();int64 end = getTickCount();std::cout<<"opencv: "<<(end-start)/getTickFrequency()<<std::endl;

4,利用Qt中的QTime

QTime time;time.start();function();std::cout<<"time is:"<<time.elapsed()<<std::endl;

通过比较这几种计时方法,分为几种情况:

1,在单线程工作中,计时方法准确度几乎是一致的。

2,在多线程时候,opencv的方法和qtime的方法保持一致,clock_t计时方法和其他有一定出入。

3,在GPU参与运算的时候,opencv方法和qtime方法保持一致。

以上的计时结果都是大同小异,差别不是很大。底层的计时方法是计算cpu的时钟。

原创粉丝点击