无法将line_探查器与Cython一起使用

2024-10-04 05:20:37 发布

您现在位置:Python中文网/ 问答频道 /正文

基于对this question的回答,我试图将line_profiler与cythoized函数一起使用。在

关于上述问题,公认的答案给了我们一个例子,说明如何使用它与jupyter笔记本电脑。在

但是,当我试图使用diustils构建pyx文件时,它不起作用。在

我们很明显地尝试使用

kernprof -l -v script.py

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

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

^{pr2}$

有什么想法吗?在


Tags: 文件函数答案linescriptjupyterthisprofiler
1条回答
网友
1楼 · 发布于 2024-10-04 05:20:37

profile修饰符由kernprof注入到globals命名空间中,因此在编译时不可用。但是,您可以apply the ^{} decorator to a function even after it has been defined。例如,在您的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运行它,它的行为应该与预期的一样。在

相关问题 更多 >