测量时间在C中的函数 [英] measure time in a function in C

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

问题描述

我调试C应用程序,我想知道多少时间花费在一个特定的功能。

I'm debugging an C application and I'd like to know how much time it spends in a particular function.

我可以改变源$ C ​​$ c和添加一些code做了测量,但它似乎没有我的权利。我宁愿与外部应用程序去做,而无需重新编译每次。

I could change the source code and add some more code to do the measurement, but it doesn't seem right to me. I'd rather do it with external application, without recompiling every time.

我发现有可能成立GDB一个破发点,所以我想,那一定是有可能利用简单的程序类似的工具来跟踪时间:
  - 设置断点
  - 停止时,测量实际时间和运行功能
  - 离开功能时,重新测量时间
但是,我还没有找到一种方法,如何做到这一点的GDB:(

I found out it's possible to set up a break point in GDB so I thought, it must be possible to track the time using similar tool by simple procedure: - set breakpoint - when stopped, measure actual time and run the function - when leaving function, measure time again However, i haven't found a way how to do this in gdb :(

什么想法?谢谢

推荐答案

gprof的是唯一可靠的 - 在我的经验,只有在所有工作 - 如果你静态链接 -pg 的每个库的编译版本,包括C库。你可以的尝试的做到这一点使用gcc的 -profile 选项(它做什么 -pg 不加试图在子 -pg 库),但问题是,GNU libc中真的不喜欢被静态链接,你的发行版可能不提供 -pg 编译版本每次你需要的库。

gprof is only reliable -- in my experience, only works at all -- if you statically link -pg compiled versions of every library, including the C library. You can try to do this using gcc's -profile option (which does what -pg does plus tries to sub in -pg libraries) but the problem is, GNU libc really does not like being statically linked, and your distro may not provide -pg compiled versions of every library you need.

我建议你试试 cachegrind ,这是一个的valgrind 的操作模式,只需要调试信息的一切。这是很容易得到。美中不足的是,它拥有巨大的成本;如此巨大,这可能会使对设备的测试。期望至少2倍的增速放缓。

I suggest you try cachegrind, which is a valgrind mode of operation and only needs debug info for everything. That's much easier to get. The catch is, it has huge overhead costs; so huge that it might invalidate your testing. Expect at least a 2x slowdown.

您也可以尝试 PERF - 如果你能得到你的手放在一个副本。这是非常聪明的,但它是通过和内核黑客谁认为人们的一样从头的建筑物的东西。我有很复杂的运气吧。 (务必阅读 http://web.eecs.utk.edu/~vweaver1 /项目/穿孔 - 事件/ 这是关于底层API,而不是实用,但可能仍保存你的时间浪费很大。)

You can also try perf -- if you can get your hands on a copy. It's very clever, but it is of, by, and for kernel hackers who think people like building stuff from scratch. I have had very mixed luck with it. (Do read http://web.eecs.utk.edu/~vweaver1/projects/perf-events/ which is about the underlying API, not the utility, but might still save you from a great deal of wasted time.)

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

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