如何配置联合Python和C code [英] How to profile combined python and c code

查看:89
本文介绍了如何配置联合Python和C code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由多个Python脚本的应用。有些脚本被调用C code。该应用程序正在运行远远低于它,所以我想它配置文件,看看问题出在哪里。是否有一个工具,软件包或只是一种方法来分析这样的应用程序?将遵循蟒蛇code到C code和型材这些电话以及一个工具?

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?

注1:我非常清楚的标准的Python分析工具。我专门找这里结合的Python / C分析。

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

注2:Python模块呼吁使用ctypes的C code(请参阅 HTTP://docs.python。组织/库详细信息/ ctypes.html )。

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

谢谢!

推荐答案

<一个href=\"http://stackoverflow.com/questions/375913/what-can-i-use-to-profile-c-$c$c-in-linux/378024#378024\">Stackshots工作。既然你已经联合Python和C可以分别处理它们。对于Python,你可以按下Ctrl-C,而它的速度缓慢,研究堆。这样做几次。这将揭露任何你可以在Python code修复。对于C code,运行整个事情像GDB调试程序下,然后按Ctrl-C获得C中的堆栈跟踪其中的一些会揭露任何你可以在C code修复。我告诉OProfile的也可以做到这一点。 (另一种方法是使用 lsstack 如果可用)。

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.)

这是这一原则的作品一个鲜为人知的方法:假设你有一个无限循环或接近无限循环。你将如何找到它?你会停止程序,看看它在做什么,对不对?假设该方案只用了两倍的时间是必要的。每次暂停时间它,你会抓住它做不必要的事情的几率是50%。因此,所有你需要做的是停止它的次数。当你看到它做一些可以改进的,在少至2个样品,你知道你可以修复,对于一个健康的加速。然后,你可以重复它来获得下一个问题。测量是不是问题的关键。醒目的东西,你可以提高才是重点。

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.

这篇关于如何配置联合Python和C code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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