Windows 10 + VM Ware 16 + Ubuntu 20.04
Ubuntu环境下测试了两种测量代码运行时间的函数,二者精度接近,结果相似,但是个人更倾向于第一种,更简洁。
一、clock()
单位是秒s,精度是ms
#include <time.h>
int main(void)
{
double duration;
double start = clock();
// 带测算时间的代码
double finish = clock();
duration = (finish - start) / CLOCKS_PER_SEC;
printf("%f seconds\n", duration);
return 0;
}
二、gettimeofday()
输出单位是s,精度是ms
#include <time.h>
struct timeval start1,end1;
gettimeofday(&start1, NULL );
// 带测算时间的代码
gettimeofday(&end1, NULL );
long timeuse =1000000 * ( end1.tv_sec - start1.tv_sec ) + end1.tv_usec - start1.tv_usec;
printf("time=%f\n",timeuse /1000000.0);
三、pcl::console::TicToc time
#include <pcl/console/time.h>
pcl::console::TicToc time;
time.tic();
/*代码段*/
cout << "-> 时间: " << time.toc()/1000 <<"s"<<endl;