简体   繁体   English

测量Linux中应用程序的运行时

[英]Measuring the Runtime of Applications in Linux

I have a bunch of applications which are compiled via their own Makefiles. 我有一堆通过自己的Makefile编译的应用程序。 Could you please let me know what can be the best way to measure their runtime? 您能否让我知道衡量其运行时间的最佳方法是什么? I have tried "time" command in linux but it does not have that much accuracy and stability in the result. 我已经在Linux中尝试了“时间”命令,但是结果没有那么多的准确性和稳定性。 I also tried to add clock_gettime() function, however, there were two issues associated with it. 我也尝试添加clock_gettime()函数,但是,有两个相关的问题。 1) The number of applications are too much to do this. 1)应用程序数量太多,无法做到这一点。 2) Even if I can add clock_gettime() to all of the applications, I do not know how to add "-lrt" to their Makefiles. 2)即使我可以将clock_gettime()添加到所有应用程序中,我也不知道如何在其Makefile中添加“ -lrt”。

Thanks 谢谢

I have used gettimeofday() to successfully time my executions on ubuntu: 我已经使用gettimeofday()来成功计时我在ubuntu上的执行时间:

#include <sys/time.h>

int main()
{
float msec=0;
struct timeval start,end;
char* persons[] = {"5#8","5#7","6#0","5#7"};

//printf("%d",get_height(persons));

gettimeofday(&start,NULL);
for(int z=0;z<20000;z++)
    get_height(persons);
usleep(1000000);
gettimeofday(&end,NULL);

msec = (end.tv_sec-start.tv_sec)*1000 + (end.tv_usec-start.tv_usec)/(float)1000;
printf("avg time taken: %0.2f\n",msec/(float)20000);

}

Read time(7) man page. 阅读time(7)手册页。

And most importantly, run several times the program you want to benchmark. 最重要的是,运行几次要基准测试的程序。 If it happens to access files (which is common), the files could be in the system cache (with a faster access). 如果碰巧访问文件(这很常见),则文件可能位于系统缓存中(访问速度更快)。

I find the time(1) good enough. 我发现time(1)足够好。 It has many options. 它有很多选择。 And you should repeat at least 3 or 4 times the run. 并且您应该至少重复运行3到4次。 Try perhaps time yourprogram or /usr/bin/time -v yourprogram 尝试或许time yourprogram/usr/bin/time -v yourprogram

clock_gettime(2) is only needed inside some program (which you would modify to call it). clock_gettime(2)仅在某些程序中需要(您可以对其进行修改以调用它)。 With recent kernels and libc (eg kernel 3.9 and libc 2.17) you don't need anymore to link -lrt to get it (it is now in libc ) 使用最新的内核和libc (例如内核3.9和libc 2.17),您不再需要链接-lrt来获取它(它现在在libc

You could also use gettimeofday or clock 您还可以使用gettimeofdayclock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM