分析 python C 扩展 [英] Profiling python C extensions

查看:37
本文介绍了分析 python C 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个 python C 扩展,它从 python 接收数据并计算一些 cpu 密集型计算.可以分析 C 扩展吗?

I have developed a python C-extension that receives data from python and compute some cpu intensive calculations. It's possible to profile the C-extension?

这里的问题是,用 C 编写要分析的示例测试将具有挑战性,因为代码依赖于特定的输入和数据结构(由 python 控制代码生成).

The problem here is that writing a sample test in C to be profiled would be challenging because the code rely on particular inputs and data structures (generated by python control code).

您有什么建议吗?

推荐答案

我找到了使用 google-perftools.诀窍是将函数 StartProfiler 和 StopProfiler 包装在 python 中(在我的例子中是通过 cython).

I've found my way using google-perftools. The trick was to wrap the functions StartProfiler and StopProfiler in python (throught cython in my case).

分析 C 扩展足以将 Python 代码包装在 StartProfiler 和 StopProfiler 调用中.

To profile the C extension is sufficient to wrap the python code inside the StartProfiler and StopProfiler calls.

from google_perftools_wrapped import StartProfiler, StopProfiler
import c_extension # extension to profile c_extension.so

StartProfiler("output.prof")
... calling the interesting functions from the C extension module ...
StopProfiler()

然后分析例如你可以以callgrind格式导出并在kcachegrind中查看结果:

Then to analyze for example you can export in callgrind format and see the result in kcachegrind:

pprof --callgrind c_extension.so output.prof > output.callgrind 
kcachegrind output.callgrind

这篇关于分析 python C 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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