10

根据对这个问题的回答,我试图将line_profiler与 cythonized 函数一起使用。

在上述问题上,接受的答案为我们提供了如何将它与 jupyter notebook 一起使用的示例。

但是,当我尝试pyx使用 disutils 构建文件时,它不起作用。

我们我明确地尝试使用运行脚本

kernprof -l -v script.py

它只返回Timer unit经过的时间。

如果我尝试使用 修饰 cython 文件上的函数@profile,它不会编译返回:

undeclared name not builtin: profile

有任何想法吗 ?

4

1 回答 1

6

装饰器profile被注入globals命名空间,kernprof因此在编译时不可用。但是,即使在定义后,您也可以profile装饰器应用于函数。例如,在您的中,script.py您可以编写以下内容。

from cython_module import function_to_be_profiled
# Apply the `profile` decorator
function_to_be_profiled = profile(function_to_be_profiled)

# Use function_to_be_profiled as intended

如果您使用标准 python 运行脚本,则代码段的第三行将失败,即python script.py,因为profile未定义装饰器。但是,如果您使用kernprof.

于 2018-08-13T18:41:33.937 回答