2017-01-29 6 views
2

에 line-profiler (ipython에서)를 사용했습니다.이 질문에 대한 답변을 How to profile cython functions line-by-line으로 읽었지만 설정이 제대로 작동하지 않습니다. 컴파일 된 Cython 코드

은 내가 cumsum.pyx 파일이 : 나는하지 않는

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

을 :

cython cumsum.pyx 
gcc cumsum.c $(pkg-config --cflags --libs python3) -o cumsum.so -shared -fPIC 

가 그럼 난 ipython에 프로필을 시도 :

# 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 

나는 그것을 컴파일 오류 메시지, 빈 프로필 만 :

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를 사용, 그래서 빌드 시스템을 무신론자 솔루션을 부탁드립니다

documentation on Cythons "Profiling" 이미 CYTHON_TRACE 매크로 설정하는 방법을 예를 포함

답변

0

이 문제가 DEF CYTHON_TRACE = 1 실제로 바로 일정을 설정하지 않는 것이 었습니다 밝혀졌습니다.

해결 방법은 다음과 같습니다 GCC 라인 변경의 distutils

2. 를 사용

1. MSeifert's answer,

gcc cumsum.c $(pkg-config --cflags --libs python3) -o cumsum.so -shared -fPIC -DCYTHON_TRACE=1 

3. 에 추가 헤더 trace.h을 만들고 거기에 일정을 설정

#define CYTHON_TRACE 1 
추가와 함께

add_definitions(-DCYTHON_TRACE) 
1

:

# distutils: define_macros=CYTHON_TRACE_NOGIL=1 

대신하여 DEF CYTHON_TRACE = 1을 .

%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 

을 그리고 프로파일 링을 보여 주었다 :

은 내가 %%cython를 사용하여 컴파일 할 때 일

%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 
+0

감사를 추가, cumsum.pyx

cdef extern from "trace.h": pass 
CMake와

4. 에 다음과 같습니다. 나는 그것을 시도했지만 문제를 해결하지 못합니다. – user357269

+0

좀 더 구체적으로 설명해주십시오. 뭐가 문제 야? 'cythonize()'를 사용 했습니까? – MSeifert

+0

아, 죄송합니다. 원래 게시물에서 변경된 사항이 없으면 프로필이 비어 있습니다. 당신이 그것을 "미리 컴파일"한다면 그것은 당신에게 효과가 있습니까? – user357269