您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:22:33 python 字数 6075 阅读 548 来源 www.jb51.cc/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):

概述

这是我的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取得了巨大的成功,所以很明显错误必须在于我.任何人都可以对此有所了解并告诉我我做错了什么?
谢谢!

我做了以下.我创建了一个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

总结

以上是编程之家为你收集整理的对于基本求和计算,Cython并不比Python快得多全部内容,希望文章能够帮你解决对于基本求和计算,Cython并不比Python快得多所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶