测量程序的运行时间
我需要一个工具来测量程序的运行时间,比如 gprof。但gprof的分辨率不够好(大约0.01秒)。 oprofile 似乎可以做到这一点,我会尝试学习如何获取有关时间信息的数据,但我不能。
那么,谁可以告诉我具体操作步骤,或者有人知道其他工具可以做同样的事情吗?
I need a tool to measure a program's running time, like gprof. But the resolution of gprof is not good enough (about 0.01 second). oprofile seems can do it, I will try to learn how to get the data about the time info, but I can't.
So, who can tell me the steps how to do it, or anyone knows other tool can do the same thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
测量整个程序执行的运行时间在高分辨率下很少有用;当您分析事物时,您通常不想包含太多的开销。
通常最好只测量某些关键路径的执行时间,即使这样,多次重复执行该路径通常也是一个好主意,以提高计时准确性。
在Linux/POSIX系统下,经常使用
gettimeofday()
对于这样的计时,它具有微秒精度:上面假设
my_interesting_function()
是您要测量其性能的函数。当然,根据函数的实际运行时间调整重复次数。Measuring runtime over a whole program's execution is seldom useful with high resolution; there's too much overhead that you generally don't want to include when you're profiling things.
It's often better to measure the execution time of some critical path only, and even then it's often a good idea to repeat the execution of that path many times, to improve timing accuracy.
Under Linux/POSIX systems,
gettimeofday()
is often used for such timing, it has microsecond precision:The above assumes that
my_interesting_function()
is the function whose performance you want to measure. Of course tweak the number of repetitions depending on the actual runtime of the function.尽管这是一篇旧帖子,但我想我应该发布一些我认为对于测量进程运行时间非常有用的代码,因为它可能对其他人有用。
然后可以使用以下功能:
可以轻松扩展代码以利用 rusage 提供的其他巧妙的东西(请参阅 http://www.gnu.org/s/libc/manual/html_node/Resource-Usage.html 了解更多详细信息)。希望有人会觉得有用:)
问候,
瓦西尔
even though it's an old post, I thought I'd post some code I find quite useful for measuring runtime of a process, since it might be useful to someone else.
Then one could use the functions like:
Code can easily be extended to take advantage of the other neat stuff rusage offers (see http://www.gnu.org/s/libc/manual/html_node/Resource-Usage.html for more details). Hope someone will find that useful :)
Regards,
Vassil
如果您使用的是 Linux/MacOSX/Unix,您始终可以使用
time
命令。即使在超级计算机上它也相当准确。时间以秒为单位记录,精确到 3 位数字(即:1.034 秒)。唯一的问题是它将测量应用程序的整个运行时间,您不能仅测量子例程。
用法示例:
否则您将不得不编写一个计时器类。
If you are on Linux/MacOSX/Unix you can always use the
time
command.It is fairly accurate even on a super computer. Time is recorded in seconds down to 3 digits of precision (ie: 1.034s). The only issue is that it will be measuring the whole run time of the application, you cannot measure sub routines only.
example usage:
Otherwise you will have to write a timer class.
如果您只想计时,请迭代 10^3 或 10^6 次,如 unwind 所说,然后秒表。 (我确实使用我的手表。)
如果您想找出导致速度变慢的原因(一个非常不同的问题),您可以比 gprof 做得更好。 试试这个。
如果您有兴趣,这里是对 gprof 的批评。
If you just want to time it, iterate it 10^3 or 10^6 times, as unwind said, and stopwatch it. (I literally use my watch.)
If you want to find out what is making it slow (a very different problem) you can do better than gprof. Try this.
If you're interested, here's a critique of gprof.