Linux下打印 C/C++ 程序运行的时间

使用 getTickCount

头文件 #include
C++ 程序:

double t = (double)getTickCount();
// do something ...
t = ((double)getTickCount() - t)/getTickFrequency();
cout << "Times passed in seconds: " << t << endl;

C 程序:

clock_t start, finish;
double totaltime;
start = clock();
//do something ...
finish = clock();
totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
printf("Time passed in seconds: %f\n", totaltime);

使用 time 命令

示例:

/nfsroot/opencv_test # time ./time
Times passed in seconds: 4.55756
real    0m 4.61s
user    0m 4.27s
sys     0m 0.02s

可以大致认为程序执行的时间是 user 加上 sys ,其中 real 时间大于我们打印出来的程序执行时间是因为程序在执行时有系统阻塞,系统暂时去执行其他任务了。

你可能感兴趣的:(C/C++,linux,嵌入式,程序时间)