如何分析组合的 python 和 c 代码

发布于 2024-09-29 13:17:43 字数 386 浏览 2 评论 0原文

我有一个由多个 python 脚本组成的应用程序。其中一些脚本正在调用 C 代码。该应用程序现在的运行速度比以前慢得多,因此我想对其进行分析以查看问题所在。是否有工具、软件包或只是一种分析此类应用程序的方法?有一个工具可以将 python 代码转换为 C 代码并分析这些调用吗?

注 1:我非常了解标准的 Python 分析工具。我在这里专门寻找组合的 Python/C 分析。

注 2:Python 模块使用 ctypes 调用 C 代码(请参阅 http://docs.python.org /library/ctypes.html 了解详细信息)。

谢谢!

I have an application that consists of multiple python scripts. Some of these scripts are calling C code. The application is now running much slower than it was, so I would like to profile it to see where the problem lies. Is there a tool, software package or just a way to profile such an application? A tool that will follow the python code into the C code and profile these calls as well?

Note 1: I am well aware of the standard Python profiling tools. I'm specifically looking here for combined Python/C profiling.

Note 2: the Python modules are calling C code using ctypes (see http://docs.python.org/library/ctypes.html for details).

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉城メ夜风 2024-10-06 13:17:43

Stackshots 有效。由于您已经组合了 Python 和 C,因此您可以单独处理它们。对于 Python,您可以在检查堆栈速度很慢时按 Ctrl-C。这样做几次。这将暴露您可以在 python 代码中修复的任何内容。对于 C 代码,在 GDB 之类的调试器下运行整个程序,然后按 Ctrl-C 以获得 C 中的堆栈跟踪。其中一些将公开您可以在 C 代码中修复的任何内容。我听说 OProfile 也可以做到这一点。 (另一种方法是使用lsstack(如果可用)。)

这是一种鲜为人知的方法,其工作原理如下:假设您有一个无限循环或接近无限循环。你会怎么找到它?您会停止该程序并查看它在做什么,对吧?假设该程序只花费了所需时间的两倍。每次你停止它时,你发现它做不必要的事情的几率是 50%。所以你所要做的就是多次停止它。一旦您看到它在短短 2 个样本上做了一些可以改进的事情,您就知道您可以修复它以实现健康的加速。然后你可以重复它来解决下一个问题。测量不是重点。抓住你可以改进的地方才是重点。

Stackshots work. Since you have combined Python and C you can handle them separately. For Python, you can hit Ctrl-C while it's being slow to examine the stack. Do this several times. That will expose anything you can fix in the python code. For the C code, run the whole thing under a debugger like GDB and hit Ctrl-C to get a stack trace in C. Several of those will expose anything you can fix in the C code. I'm told OProfile can also do this. (Another way is to use lsstack if it is available.)

This is a little-known method that works on this principle: Suppose you have an infinite loop or a nearly infinite loop. How would you find it? You would halt the program and see what it was doing, right? Suppose the program only took twice as long as necessary. Each time you halted it, the chance that you would catch it doing the unnecessary thing is 50%. So all you have to do is halt it a number of times. As soon as you see it doing something that could be improved, on as few as 2 samples, you know you can fix that for a healthy speedup. Then you can repeat it to get the next problem. Measuring is not the point. Catching things you can improve is the point.

傾旎 2024-10-06 13:17:43

这种组合会非常困难,但您可以使用一些标准分析器,例如 valgrind、gprof 甚至 oprofile(尽管我从未成功过)从中获得有意义的输出)。

The combination would be pretty hard, but you can use some of the standard profilers like valgrind, gprof or even oprofile (although I never managed to get meaningful output out of it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文