对于基本求和计算,Cython并不比Python快得多

hob*_*777 6 python python-2.x cython anaconda

我试图按照Continuum Analytics博客上的一个例子来对Python,Cython,Numba进行基准测试,以获得使用for循环计算的总和.不幸的是,我发现Cython比Python慢​​!

这是我的Python函数定义:

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

现在我的Cython功能:

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
Run Code Online (Sandbox Code Playgroud)

现在我有一个脚本可以提取两个函数和基准:

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)
Run Code Online (Sandbox Code Playgroud)

现在我的输出是:

Python Sum    (ms): 9.44624
Cython     (ms): 8.54868
Run Code Online (Sandbox Code Playgroud)

基于上面链接的博客文章中的图表,我期待速度提高100倍-1000倍,但我所看到的只是Cython比vanilla Python略快.

我在这里做错了吗?这似乎是一个非常基本的问题,具有简单的函数定义,很明显很多人使用Cython取得了巨大的成功,所以很明显错误必须在于我.任何人都可以对此有所了解并告诉我我做错了什么?谢谢!

Cla*_*diu 10

我不确定你为什么会得到那个结果.正如一位评论者所说,你的代码原则上不应该起作用,因为你将floats 传递给一个期望ints 的函数.也许你cython_sum.py在同一目录中留下了一个文件?

我做了以下.我创建了一个python_sum.py,其中包含您的确切定义python_sum.然后我稍微修改了你的Cython代码:

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
Run Code Online (Sandbox Code Playgroud)

我创建了一个安装文件来构建Cython模块:

setup.py:

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

setup(
  name = 'Cython sum test',
  ext_modules = cythonize("cython_sum.pyx"),
)
Run Code Online (Sandbox Code Playgroud)

我使用了构建模块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)
Run Code Online (Sandbox Code Playgroud)

我得到了以下结果:

$ python test.py
Python Sum    (ms): 4111.74
Cython        (ms): 7.06697
Run Code Online (Sandbox Code Playgroud)

现在这是一个很好的加速!


此外,按照此处列出的指导原则,我可以获得额外的(小)加速:

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
Run Code Online (Sandbox Code Playgroud)

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()],
)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

结果:

$ python test.py
Cython naive  (ms): 676.437
Cython fast   (ms): 645.797
Run Code Online (Sandbox Code Playgroud)