函数执行时间 [英] Function execution time

查看:131
本文介绍了函数执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在Linux上用C ++编写的函数的执行时间。我发现了很多有关的帖子。我尝试了此链接中提到的所有方法计时器方法计算时间。以下是我的函数执行时间的结果:

I want to find out the execution time of my function written in C++ on Linux. I found lots of posts regarding that. I tried all the methods mentioned in this link Timer Methods for calculating time. Following are the results of execution time of my function:

time() :           0 seconds 
clock() :          0.01 seconds
gettimeofday() :   0.002869 seconds
rdtsc() :          0.00262336 seconds
clock_gettime() :  0.00672151 seconds
chrono :           0.002841 seconds 

请帮助我,哪种方法在其读数可靠,因为所有的结果在他们的读数不同。我读到你的操作系统在不同的任务之间切换,所以读数不能期望是非常准确的。有一种方法,我可以计算CPU花在我的功能上的时间。我听说使用profiling工具,但还没有找到任何只是一个函数的例子。请指导我。

Please help me which method is reliable in its readings as all the results differ in their readings. I read that your OS is switching between different tasks so the readings cannot be expected to be very accurate. Is there a way that I can just calculate the time CPU spends on my function. I heard about the use of profiling tool but have not found any example for just a function yet. Please guide me.

推荐答案

阅读 time(7)

由于各种原因

因此,添加一些循环,重复您的函数多次,或更改其输入使其运行时间更长。确保总计划的执行时间(由时间(1) ...)至少是大约一秒(如果可能,请确保至少有半秒的CPU 时间)。

So, add some loop repeating your function many times, or change its input so that it runs longer. Ensure that the execution time of the total program (given by time(1)...) is at least a second approximately (if possible, ensure that you have at least half a second of CPU time).

对于性能分析,请编译并链接到 g ++ -Wall -pg -O1 ,然后使用 gprof(1)(有更复杂的配置方式,例如 oprofile ...)。

For profiling, compile and link with  g++ -Wall -pg -O1 then use gprof(1) (there are more sophisticated ways to profile, e.g. oprofile ...).

另请参见此回答到非常类似的问题(由相同的 Zara )。

See also this answer to a very similar question (by the same Zara).

这篇关于函数执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆