博客专栏

EEPW首页>博客> linux下统计程序/函数运行时间

linux下统计程序/函数运行时间

发布人:电子禅石 时间:2019-07-20 来源:工程师 发布文章
一. 使用time 命令

例如编译一个hello.c文件

#gcc hello.c -o hello

生成了hello可执行文件,此时统计该程序的运行时间便可以使用如下命令

#time ./hello
在程序运行结束后便会显示出所需时间

real 0m2.913s user 0m0.012s sys 0m0.508s

二. 使用clock()函数统计

复制代码
1 #include 2 #include  /*要包含的头文件*/ 3 4 int main(int argc, char *argv[]) 5 { 6 /* Init */ 7 clock_t start, end; 8 start = clock(); /*记录起始时间*/ 9 10 printf("time calc test\n");11 /*12 *13 *14 * 函数进行的一些列操作15 *16 * */17 18 /* Final Status */19 end = clock(); /*记录结束时间*/20 {21 double seconds =(double)(end - start)/CLOCKS_PER_SEC;22 fprintf(stderr, "Use time is: %.8f\n", seconds);23 }24 return 0;25 }
复制代码

运行结果:

复制代码
# time ./helloTest time calc test Use time is 0.00003100real 0m0.003s user 0m0.000s sys 0m0.000s
复制代码

CLOCKS_PER_SEC用于将clock()函数的结果转化为以秒为单位的量

三. 优缺点对比

time命令在不修改代码的情况下记录程序运行时间,但是,从上面对比可看出time命令统计的结果比较粗糙。
另外,time命令,统计的结果包涵程序加载和退出的时间。因此,若想得出函数运行时间较为准确的结果,建议使用clock()函数。
若想了解整个项目中各个函数的运行时间,以期获得性能提升,建议使用——开源工具

转自:http://blog.csdn.net/davie1love/article/details/47087475


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

三维扫描仪相关文章:三维扫描仪原理


关键词:

相关推荐

技术专区

关闭