如何逐行分析cython功能 [英] How to profile cython functions line-by-line
问题描述
我经常很难在我的 cython
代码中找到瓶颈。如何逐行描述 cython
函数?
I often struggle to find bottlenecks in my cython
code. How can I profile cython
functions line-by-line?
推荐答案
Robert Bradshaw帮助我获得了Robert Kern的 line_profiler
工具,该工具可用于 cdef
函数,我想我可以在 stackoverflow
。
Robert Bradshaw helped me to get Robert Kern's line_profiler
tool working for cdef
functions and I thought I'd share the results on stackoverflow
.
简而言之,请设置常规的 .pyx
文件,构建脚本并在调用 cythonize
之前添加以下内容。
In short, set up a regular .pyx
file and build script and add the following before your call to cythonize
.
# Thanks to @tryptofame for proposing an updated snippet
from Cython.Compiler.Options import get_directive_defaults
directive_defaults = get_directive_defaults()
directive_defaults['linetrace'] = True
directive_defaults['binding'] = True
此外,您需要定义C宏 CYTHON_TRACE = 1
通过修改您的扩展名
设置,使
Furthermore, you need to define the C macro CYTHON_TRACE=1
by modifying your extensions
setup such that
extensions = [
Extension("test", ["test.pyx"], define_macros=[('CYTHON_TRACE', '1')])
]
使用的工作示例
魔术在这里: iPython
笔记本中的%% cython
http://nbviewer.ipython.org/gist/tillahoffmann/296501acea231cbdf5e7
A working example using the %%cython
magic in the iPython
notebook is here:
http://nbviewer.ipython.org/gist/tillahoffmann/296501acea231cbdf5e7
这篇关于如何逐行分析cython功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!