如何解释 gprof 输出
我刚刚用 gprof 分析了我的程序并得到了这个:
100.01 0.01 0.01 23118 0.43 0.43 std::vector<int, std::allocator<int> >::operator=(std::vector<int, std::allocator<int> > const&)
这让我很困惑,因为它说它 100.01% 的时间都在使用 = 运算符。我的猜测是否正确,这意味着它只是一直复制数据,并且程序允许使用的内存是否有限制?
I just profiled my program with gprof and got this:
100.01 0.01 0.01 23118 0.43 0.43 std::vector<int, std::allocator<int> >::operator=(std::vector<int, std::allocator<int> > const&)
Which confuses me, as it says that it is using 100.01% of the time using the = operator. Am i right guessing that this means it is just copying data all along and is there a limit of how much memory a program is allowed to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您分析的运行时间似乎太短,无法获取任何有用的数据。
gprof
的工作方式是定期中断您的代码以查看您当时所处的函数。如果代码运行时间不是很长,它可能只会收集少量数据点。相比之下,callgrind
检测您的代码并跟踪每个代码函数调用和返回并检查钩子之间花费了多少时间。即使行程很短,这也能提供更全面的视野。这有一个缺点,它会大大减慢程序的速度。callgrind
的另一个优点是你可以停止,启动并保存其数据收集。因此,如果您不想监视程序的启动或只想捕获它执行特定操作的时间,您可以这样做。另外,有一个名为kcachegrind
的工具,可以为您提供您收集的数据的出色图形视图。如果您可以容忍在运行程序进行测试时将程序速度减慢四倍,请改用
callgrind
——它是valgrind
的一部分。如果您使用的是 Linux,您的发行版可能有一个
valgrind
软件包和一个包含kcachegrind
的软件包(可能称为 kdesdk 或其他)。花时间学习如何使用它们是值得的(它们不像gprof
那样容易上手)。我想你会发现 kcachegrind 的 GUI 令人印象深刻(看看这个 屏幕截图)。而且,正如其名称所暗示的,它也可以分析cachegrind
输出。It looks like you profiled too short a run to get any useful data.
The way
gprof
works is that it periodically interrupts your code to see what function you were in at that instant. If the code isn't running very long, it may only collect a small number of data points. In contrast,callgrind
instruments your code and tracks each function call and return and checks how much time was spent between hooks. This gives it a much fuller view, even if the run is short. This has a downside, it slows the program down quite a bit.Another advantage of
callgrind
is that you can stop, start, and save its data collection. So if you don't want to monitor your program's startup or want to catch only the time it's doing a particular thing, you can. Plus, there's a tool calledkcachegrind
that can give you a great graphical view of the data you collected.If you can tolerate slowing your program down by a factor of four while you're running it for testing, use
callgrind
instead -- it's part ofvalgrind
.If you're using Linux, your distribution probably has a
valgrind
package and a package that includeskcachegrind
(which may be called kdesdk or something else). It's worth the time invested to learn how to use them (they are not as easy to get started with asgprof
). I think you'll find thekcachegrind
's GUI impressive (look at this screenshot). And, as its name suggests, it can analyzecachegrind
output too.