C语言 使用 valgrind 了解每个函数花费的时间(以秒为单位)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6663614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:07:15  来源:igfitidea点击:

use valgrind to know time(in seconds) spent in each function

cprofilingvalgrind

提问by Syntax_Error

is there any extension of valgrind, that can be used in the command window, that would help me know the time, in seconds, spent in each function in my C code?

是否有任何可以在命令窗口中使用的 valgrind 扩展,这将帮助我知道我的 C 代码中每个函数花费的时间(以秒为单位)?

thanks =)

谢谢=)

采纳答案by Karoly Horvath

For machine instruction profiling use valgrind's callgrind(also, cachegrindcan do cache and branch prediction profiling which is quite nice).

对于机器指令分析,请使用 valgrind 的callgrind(此外,cachegrind可以进行缓存和分支预测分析,这非常好)。

For time measurements use google's cpu profiler, it gives way better results than gprof. You can set sampling frequency and it can show the output as a nice annotated call graph.

对于时间测量,使用google 的 cpu profiler,它提供比 gprof 更好的结果。您可以设置采样频率,它可以将输出显示为漂亮的带注释的调用图。

回答by Frank Osterfeld

Valgrind isn't suited for measuring time, as running an application in valgrind distorts the results (slowdown, CPU vs. I/O). Thus valgrind profiling tool callgrind doesn't measure time but CPU instructions. Callgrind is only useful if your bottleneck is CPU-bound (thus CPU instructions matter), then CPU instructions measured will be in proportion to the time spent. It's not useful if heavy I/O or multiple processes are involved. Then you should use a sampling profiler, like sysprof or gprof (Edit 2020: perf). That checks in intervals which function the process is in, with less distorted results.

Valgrind 不适合测量时间,因为在 valgrind 中运行应用程序会扭曲结果(速度减慢、CPU 与 I/O)。因此,valgrind 分析工具 callgrind 不测量时间,而是测量 CPU 指令。Callgrind 仅在您的瓶颈受 CPU 限制(因此 CPU 指令很重要)时才有用,然后测量的 CPU 指令将与花费的时间成正比。如果涉及大量 I/O 或多个进程,则它没有用。然后您应该使用采样分析器,例如 sysprof 或 gprof(Edit 2020:perf)。这会检查过程所处的时间间隔,结果失真较小。

回答by Sriram

Use thislink. I would think something like Callgrindshould do the trick.

使用链接。我认为类似的事情Callgrind应该可以解决问题。