【发布时间】:2015-04-02 19:50:56
【问题描述】:
我经常难以在我的 cython 代码中找到瓶颈。如何逐行分析cython 函数?
【问题讨论】:
-
cython 调试器是否允许您暂停它?然后你可以做this。
我经常难以在我的 cython 代码中找到瓶颈。如何逐行分析cython 函数?
【问题讨论】:
Robert Bradshaw 帮助我让 Robert Kern 的 line_profiler 工具适用于 cdef 函数,我想我会在 stackoverflow 上分享结果。
简而言之,设置一个常规的.pyx 文件并构建脚本,并在调用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
此外,您需要通过修改 extensions 设置来定义 C 宏 CYTHON_TRACE=1,以便
extensions = [
Extension("test", ["test.pyx"], define_macros=[('CYTHON_TRACE', '1')])
]
在iPython 笔记本中使用%%cython 魔法的工作示例如下:
http://nbviewer.ipython.org/gist/tillahoffmann/296501acea231cbdf5e7
【讨论】:
@profile 装饰函数,我无法使用返回 undeclared name not builtin: profile 的 disutils 编译文件
【讨论】:
-a 不会为您提供任何有关实际运行时的信息,而只会提供您是否在进行python 调用。
虽然@Till's answer 展示了使用setup.py-approach 分析 Cython 代码的方式,但此答案是关于 IPython/Jupiter notebook 中的临时分析,并且或多或少是 Cython-documentation 到 IPython/ 的“翻译”木星。
%prun-magic:
如果应该使用 %prun-magic,那么将 Cython 的编译器指令 profile 设置为 True 就足够了(此处以 Cython 文档中的示例为例):
%%cython
# cython: profile=True
def recip_square(i):
return 1. / i ** 3
def approx_pi(n=10000000):
val = 0.
for k in range(1, n + 1):
val += recip_square(k)
return (6 * val) ** .5
使用全局指令(即# cython: profile=True)是比修改全局 Cython 状态更好的方法,因为更改它会导致重新编译扩展(如果全局 Cython 状态更改则不会出现这种情况 - 旧使用旧全局状态编译的缓存版本将被重新加载/重用)。
现在
%prun -s cumulative approx_pi(1000000)
产量:
1000005 function calls in 1.860 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.860 1.860 {built-in method builtins.exec}
1 0.000 0.000 1.860 1.860 <string>:1(<module>)
1 0.000 0.000 1.860 1.860 {_cython_magic_404d18ea6452e5ffa4c993f6a6e15b22.approx_pi}
1 0.612 0.612 1.860 1.860 _cython_magic_404d18ea6452e5ffa4c993f6a6e15b22.pyx:7(approx_pi)
1000000 1.248 0.000 1.248 0.000 _cython_magic_404d18ea6452e5ffa4c993f6a6e15b22.pyx:4(recip_square)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
%lprun-magic
如果应使用行分析器(即%lprun-magic),则应使用不同的指令编译 Cython 模块:
%%cython
# cython: linetrace=True
# cython: binding=True
# distutils: define_macros=CYTHON_TRACE_NOGIL=1
...
linetrace=True 触发在生成的 C 代码中创建跟踪并暗示profile=True,因此不得另外设置。如果没有binding=True,line_profiler 就没有必要的代码信息并且需要CYTHON_TRACE_NOGIL=1,因此在使用 C 编译器编译时也会激活行分析(并且不会被 C 预处理器丢弃)。如果不应该基于每行分析 nogil-blocks,也可以使用CYTHON_TRACE=1。
现在可以如下使用它,传递函数,应该通过-f 选项进行行分析(使用%lprun? 获取有关可能选项的信息):
%load_ext line_profiler
%lprun -f approx_pi -f recip_square approx_pi(1000000)
产生:
Timer unit: 1e-06 s
Total time: 1.9098 s
File: /XXXX.pyx
Function: recip_square at line 5
Line # Hits Time Per Hit % Time Line Contents
==============================================================
5 def recip_square(i):
6 1000000 1909802.0 1.9 100.0 return 1. / i ** 2
Total time: 6.54676 s
File: /XXXX.pyx
Function: approx_pi at line 8
Line # Hits Time Per Hit % Time Line Contents
==============================================================
8 def approx_pi(n=10000000):
9 1 3.0 3.0 0.0 val = 0.
10 1000001 1155778.0 1.2 17.7 for k in range(1, n + 1):
11 1000000 5390972.0 5.4 82.3 val += recip_square(k)
12 1 9.0 9.0 0.0 return (6 * val) ** .5
line_profiler 对cpdef 函数有一个小问题:它不能正确检测函数体。 In this SO-post,显示了一种可能的解决方法。
应该知道,与“正常”运行相比,分析(所有上述行分析)会改变执行时间及其分布。在这里我们看到,对于相同的功能,根据分析类型需要不同的时间:
Method (N=10^6): Running Time: Build with:
%timeit 1 second
%prun 2 seconds profile=True
%lprun 6.5 seconds linetrace=True,binding=True,CYTHON_TRACE_NOGIL=1
【讨论】: