2014-02-18 6 views
6

for 루프를 사용하여 계산 된 합계에 대해 Continuum Analytics blog 벤치마킹 Python, Cython, Numba에서 주어진 예제를 따르려고합니다. 불행히도 Cython이 Python보다 느리다는 것을 알게되었습니다!Cython은 기본 합계 계산을위한 Python보다 훨씬 빠릅니다.

def python_sum(y): 
    N = len(y) 
    x = y[0] 
    for i in xrange(1,N): 
     x += y[i] 
    return x 

그리고 지금 내 사이 썬 기능 :

import timeit 
import numpy as np 
import cython_sum 
import python_sum 

b = np.ones(10000) 

timer = timeit.Timer(stmt='python_sum.python_sum(b)', setup='from __main__ import python_sum, b') 
print "Python Sum (ms): %g" % (timer.timeit(1)*1000) 

timer = timeit.Timer(stmt='cython_sum.cython_sum(b)', setup='from __main__ import cython_sum, b') 
print "Cython  (ms): %g" % (timer.timeit(1)*1000) 
:

def cython_sum(int[:] y): 
    cdef int N = y.shape[0] 
    cdef int x = y[0] 
    cdef int i 
    for i in xrange(1,N): 
     x += y[i] 
    return x 

지금 나는 두 가지 기능과 벤치 마크를 끌어 스크립트를 가지고

여기 내 파이썬 함수 정의의

이제 내 출력은

입니다.
Python Sum (ms): 9.44624 
Cython  (ms): 8.54868 

위 링크 된 블로그 게시물의 그래프를 토대로 볼 때, 속도는 100 배에서 1000 배까지 증가 할 것으로 예상했지만 Cython은 바닐라 파이썬보다 약간 빠릅니다.

여기 뭔가 잘못 되었나요? 이것은 간단한 함수 정의로 꽤 기본적인 질문처럼 보입니다. 분명히 많은 사람들이 Cython을 큰 성공으로 사용합니다. 분명히 오류가 나와 관련되어 있어야합니다. 아무도 이것에 대해 밝히고 내가 뭘 잘못하고 있는지 말해 줄 수 있습니까? 감사합니다.

+0

벤치마킹 하위 파입니다. 최소한 두 자리 숫자만큼 실행하십시오. – delnan

+0

내용물과 관련하여 질문이 아니거나 제목이 적합하지 않은 것 같습니다. –

+0

이 코드는 작동하지 않습니다. 'np.ones'는 float 배열을 반환하고 Cython 코드는 예외를 발생시킵니다. –

답변

10

그 이유는 확실하지 않습니다. 댓글 작성자가 말했듯이, floatint을 (를) 기대하는 함수로 전달할 것이므로 코드는 그대로 작동해서는 안됩니다. cython_sum.py 파일을 같은 디렉토리에 두었을 수도 있습니까?

나는 다음과 같이했다. python_sum에 대한 정확한 정의가 포함 된 python_sum.py을 만들었습니다.

cython_sum.pyx : 그럼 약간 사이 썬 코드를 변경

setup.py :

나는 설정 파일 사이 썬 모듈을 구축 할 수 있도록했다
def cython_sum(long[:] y): #changed `int` to `long` 
    cdef int N = y.shape[0] 
    cdef int x = y[0] 
    cdef int i 
    for i in xrange(1,N): 
     x += y[i] 
    return x 

from distutils.core import setup 
from Cython.Build import cythonize 

setup(
    name = 'Cython sum test', 
    ext_modules = cythonize("cython_sum.pyx"), 
) 

나는 python setup.py build_ext --inplace을 사용하여 모듈을 만들었습니다.

test.py : 다음으로, 일부 수정하여 테스트 코드 실행

import timeit 
import numpy as np 
import cython_sum 
import python_sum 

# ** added dtype=np.int to create integers ** 
b = np.ones(10000, dtype=np.int)  

# ** changed .timeit(1) to .timeit(1000) for each one ** 
timer = timeit.Timer(stmt='python_sum.python_sum(b)', setup='from __main__ import python_sum, b') 
print "Python Sum (ms): %g" % (timer.timeit(1000)*1000) 

timer = timeit.Timer(stmt='cython_sum.cython_sum(b)', setup='from __main__ import cython_sum, b') 
print "Cython  (ms): %g" % (timer.timeit(1000)*1000) 

을 그리고 나는 다음과 같은 결과를 얻었다 : 그 멋진 속도 업 지금

$ python test.py 
Python Sum (ms): 4111.74 
Cython  (ms): 7.06697 

을 !

cython_fast_sum.pyx :

import numpy as np 
cimport numpy as np 

DTYPE = np.int 
ctypedef np.int_t DTYPE_t 

def cython_sum(np.ndarray[DTYPE_t, ndim=1] y): 
    cdef int N = y.shape[0] 
    cdef int x = y[0] 
    cdef int i 
    for i in xrange(1,N): 
     x += y[i] 
    return x 

setup_fast


는 또한, 지침 here을 설명에 따라, 나는 추가 (작은) 속도 향상을 얻을 수있었습니다. py :

from distutils.core import setup 
from Cython.Build import cythonize 
import numpy as np 

setup(
    name = 'Cython fast sum test', 
    ext_modules = cythonize("cython_fast_sum.pyx"), 
    include_dirs = [np.get_include()], 
) 
012 3,516,

test.py :

import timeit 
import numpy as np 
import cython_sum 
import cython_fast_sum 

b = np.ones(10000, dtype=np.int) 

# ** note 100000 runs, not 1000 ** 
timer = timeit.Timer(stmt='cython_sum.cython_sum(b)', setup='from __main__ import cython_sum, b') 
print "Cython naive (ms): %g" % (timer.timeit(100000)*1000) 

timer = timeit.Timer(stmt='cython_fast_sum.cython_sum(b)', setup='from __main__ import cython_fast_sum, b') 
print "Cython fast (ms): %g" % (timer.timeit(100000)*1000) 

결과 :

$ python test.py 
Cython naive (ms): 676.437 
Cython fast (ms): 645.797 
+0

도움을 주신 데 대해 감사드립니다. * 타이머를 실행 * * 스크립트 에 DTYPE = np.int를 추가 cythonize 대신 확장 의 사용에 설치 파일을 변경 * .pyx 파일의 길이에 * 변경 INT : 나는 당신이 한 모든 변경을 1000 times 불행히도 내 결과는 이제 Cython에서 더욱 악화됩니다. Python Sum (ms) : 2043.02 Cython (ms) : 6394.68 – hobscrk777

+0

@ user3325401 : 음, 이상합니다. 말 그대로 새로운 디렉토리에서 시작하고, 모든 파일을 처음부터 다시 만들고, 처음부터 다시 빌드하는 등의 작업을 시도해보고 동일한 결과를 얻었습니까? – Claudiu

+0

성공! 클레 디우에게 충분히 감사 할 수는 없어요. 내 파일 참조가 모두 엉망이 됐을 것 같아. 파이썬 합계 : 2037.7 Cython (ms) : 12.1749 – hobscrk777

관련 문제