I am not sure why you got this result. As the commentator said, your code, as it is, should not even work, since you will pass the float to a function that expects int s. Maybe you left the file cython_sum.py lying in the same directory?
I did the following. I created python_sum.py that contained your exact python_sum definition. Then I changed your Cython code a bit:
cython_sum.pyx
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
I created an installation file to be able to create a Cython module:
setup.py
from distutils.core import setup from Cython.Build import cythonize setup( name = 'Cython sum test', ext_modules = cythonize("cython_sum.pyx"), )
I built the module using python setup.py build_ext --inplace . Then I checked your test code with some changes:
test.py
import timeit import numpy as np import cython_sum import python_sum
And I got the following result:
$ python test.py Python Sum (ms): 4111.74 Cython (ms): 7.06697
Now it's a nice boost!
In addition, following the recommendations here , I was able to get an additional (small) speed:
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.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()], )
test.py
import timeit import numpy as np import cython_sum import cython_fast_sum b = np.ones(10000, dtype=np.int)
Result:
$ python test.py Cython naive (ms): 676.437 Cython fast (ms): 645.797