无法与Cython一起使用line_profiler [英] Cannot use line_profiler with Cython

查看:162
本文介绍了无法与Cython一起使用line_profiler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于对这个问题的答案尝试将 line_profiler 与cythonized函数一起使用。

Based on the answer to this question I was trying to use the line_profiler with a cythonized function.

在上述问题上,被接受的答案为我们提供了如何在jupyter笔记本上使用它的示例。

On the abovementioned question, the accepted answer gives us an example on how to use it with jupyter notebook.

但是,当我尝试构建 pyx 文件使用disutils无法正常工作。

However, when I try to build the pyx file using disutils it doesn't work.

我们很显然地尝试使用

kernprof -l -v script.py

它仅返回计时器单位经过的时间。

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

If I try to decorate the function on the cython file using @profile, it doesn't compile returning:

undeclared name not builtin: profile

任何想法?

推荐答案

配置文件装饰器由 kernprof注入到 globals 命名空间中,因此在编译时不可用。但是,您可以应用配置文件装饰器到一个函数,即使它已经定义。例如,在您的 script.py 中,您可以编写以下内容。

The profile decorator is injected into the globals namespace by kernprof and is thus not available at compile time. However, you can apply the profile decorator to a function even after it has been defined. For example, in your script.py you could write the following.

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 ,因为未定义配置文件装饰器。但是,如果您使用 kernprof 运行它,它应该可以达到预期的效果。

The third line of the snippet will fail if you run the script using standard python, i.e. python script.py, because the profile decorator is not defined. But it should behave as intended if you run it using kernprof.

这篇关于无法与Cython一起使用line_profiler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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