gpt4 book ai didi

python - 不能将 line_profiler 与 Cython 一起使用

转载 作者:太空狗 更新时间:2023-10-30 00:09:17 24 4
gpt4 key购买 nike

基于对 this question 的回答我正在尝试使用 line_profiler具有 cythonized 功能。

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

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

我们显然尝试使用

运行脚本
kernprof -l -v script.py

它只返回 Timer unit 耗时。

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

undeclared name not builtin: profile

有什么想法吗?

最佳答案

profile 装饰器由 kernprof 注入(inject)到 globals 命名空间中,因此在编译时不可用。但是,您可以 apply the profile 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 运行它,它应该会按预期运行。

关于python - 不能将 line_profiler 与 Cython 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45925921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com