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

前端之家收集整理的这篇文章主要介绍了对于基本求和计算,Cython并不比Python快得多前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图按照 Continuum Analytics blog基准测试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

现在我的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

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

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)

现在我的输出是:

Python Sum    (ms): 9.44624
Cython     (ms): 8.54868

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

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

解决方法

我不确定你为什么会得到那个结果.正如一位评论者所说,你的代码原样不应该工作,因为你要将浮点数传递给一个期望整数的函数.也许你在同一目录中留下了一个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

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

setup.py:

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)',b')
print "Python Sum    (ms): %g" % (timer.timeit(1000)*1000)

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

我得到了以下结果:

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

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

此外,按照here所述的指导原则,我可以获得额外的(小)加速:

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)

# ** note 100000 runs,not 1000 **
timer = timeit.Timer(stmt='cython_sum.cython_sum(b)',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
原文链接:https://www.f2er.com/python/186417.html

猜你在找的Python相关文章