首页 > 解决方案 > 使用 line_profiler 分析 Cython 时出错

问题描述

我正在尝试对包含在 .pyx 中并通过 distutils.setup 编译的函数运行线分析器。函数及其调用可以使用 cProfile 进行分析;但是,我正试图找到一个瓶颈,并且线路分析会有所帮助。

我试图设置 Cython 以在我的 pyx 的标题中使用以下内容。

# cython: linetrace=True 
# distutils: define_macros=CYTHON_TRACE_NOGIL=1

我还尝试将 disutils 传递给编译器指令和定义的宏。

compiler_directives={'linetrace': True, 'bindings': True},
define_macros=[('CYTHON_TRACE_NOGIL', '1')])

无论我做什么,线轮廓仪都看不到该功能。我收到以下错误消息:

UserWarning: Could not extract a code object for the object <built-in function x>
  # contains convenience functions, global variable definitions, and visualization functions

运行命令行函数“kernprof”具有相同的效果,但错误消息略有不同(但显然相关)。

'builtin_function_or_method' object has no attribute '__code__'

我能够毫无问题地在 .py 文件中对非 cython 函数进行 line_profile 。

标签: pythonprofilingcython

解决方案


推荐阅读