分析python C扩展

pygabriel:

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

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

你有什么建议吗?

pygabriel:

我已经找到了使用google-perftools的方式诀窍是将函数StartProfiler和StopProfiler包装在python中(在我的情况下为cython)。

要分析C扩展名,就足以将Python代码包装在StartProfiler和StopProfiler调用内。

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中查看结果:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章