Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在已编译的Cython代码上使用line profiler(在ipython中)_Python_Cython_Line Profiler - Fatal编程技术网

在已编译的Cython代码上使用line profiler(在ipython中)

在已编译的Cython代码上使用line profiler(在ipython中),python,cython,line-profiler,Python,Cython,Line Profiler,我读了这个问题的答案,但我似乎无法让它与我的设置一起工作 我有一个cumsum.pyx文件: # cython: profile=True # cython: linetrace=True # cython: binding=True DEF CYTHON_TRACE = 1 def cumulative_sum(int n): cdef int s=0, i for i in range(n): s += i return s 我是用以下方法编写的

我读了这个问题的答案,但我似乎无法让它与我的设置一起工作

我有一个
cumsum.pyx
文件:

# cython: profile=True
# cython: linetrace=True
# cython: binding=True
DEF CYTHON_TRACE = 1

def cumulative_sum(int n):
    cdef int s=0, i
    for i in range(n):
        s += i

    return s
我是用以下方法编写的:

cython cumsum.pyx
gcc cumsum.c $(pkg-config --cflags --libs python3) -o cumsum.so -shared -fPIC
然后我尝试在
ipython
中对其进行分析:

%load_ext line_profiler
from cumsum import cumulative_sum
%lprun -f cumulative_sum cumulative_sum(100)
%load_ext cython
%%cython

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

def cumulative_sum(int n):
    cdef int s=0, i
    for i in range(n):
        s += i
    return s
我没有收到错误消息,只有一个空的配置文件:

Timer unit: 1e-06 s

Total time: 0 s
File: cumsum.pyx
Function: cumulative_sum at line 6

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     6                                           def cumulative_sum(int n):
     7                                               cdef int s=0, i
     8                                               for i in range(n):
     9                                                   s += i
    10                                           
    11                                               return s
我怎样才能让它工作

PS:我使用CMake,而不是
setup.py
,因此我希望有一个构建系统无关的解决方案

该解决方案已经包含了一个如何设置
CYTHON\u跟踪
宏的示例:

# distutils: define_macros=CYTHON_TRACE_NOGIL=1
而不是您的
DEF CYTHON\u TRACE=1

当我使用
%%cython
编译它时,它工作了:

%load_ext line_profiler
from cumsum import cumulative_sum
%lprun -f cumulative_sum cumulative_sum(100)
%load_ext cython
%%cython

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

def cumulative_sum(int n):
    cdef int s=0, i
    for i in range(n):
        s += i
    return s
并显示了配置文件:

%load_ext line_profiler
%lprun -f cumulative_sum cumulative_sum(100)
[...]
Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     7                                           def cumulative_sum(int n):
     8         1            8      8.0      3.5      cdef int s=0, i
     9         1            3      3.0      1.3      for i in range(n):
    10       100          218      2.2     94.4          s += i
    11         1            2      2.0      0.9      return s

问题是
DEF CYTHON_TRACE=1
实际上没有设置正确的常数

解决办法包括:

一,。 ,使用distutils

二,。 将gcc行更改为

gcc cumsum.c $(pkg-config --cflags --libs python3) -o cumsum.so -shared -fPIC -DCYTHON_TRACE=1
三,。 制作一个额外的头
trace.h
并在那里设置常量

 #define CYTHON_TRACE 1
将以下内容添加到
cumsum.pyx

cdef extern from "trace.h":
    pass
四,。 使用CMake,添加

add_definitions(-DCYTHON_TRACE)

谢谢我试过了,但是没有解决问题。你能说得更具体一点吗。发生了什么?您是否使用了
cythonize()
。如果你“预编译”它,它对你有用吗?啊,也许distutils行没有任何作用,因为我没有使用distutils@user357269是的,
#distutils:
-行仅在使用
cythonize
时有效(根据文档)。