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



我有一堆应用程序,它们是通过自己的Makefile编译的。你能告诉我什么是衡量它们运行时间的最佳方法吗?我在linux中尝试过"时间"命令,但结果并没有那么准确和稳定。我还尝试添加clock_gettime()函数,但是,有两个问题与之相关。1) 应用程序的数量太多,无法做到这一点。2) 即使我可以将clock_gettime()添加到所有应用程序中,我也不知道如何将"-lrt"添加到它们的Makefile中。

感谢

我已经使用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.2fn",msec/(float)20000);
}

读取时间(7)手册页。

最重要的是,要多次运行要进行基准测试的程序。如果碰巧访问文件(这很常见),文件可能在系统缓存中(访问速度更快)。

我觉得时间足够了。它有很多选择。你应该至少重复3到4次跑步。尝试time您的程序/usr/bin/time -v程序

clock_gettime(2)只在某些程序中需要(您可以修改它来调用它)。有了最近的内核和libc(例如内核3.9和libc 2.17),您就不需要再链接-lrt来获得它(它现在在libc中)

您也可以使用gettimeofdayclock

相关内容

最新更新