Python扩展和嵌入: Cython三分钟入门(笔记)

使用Cython的性能对比

作者:liuyuan_jq

2011-01-12


python代码实现


#p1.py import math def great_circle(lon1,lat1,lon2,lat2): radius = 3956 #miles x = math.pi/180.0 a = (90.0-lat1)*(x) b = (90.0-lat2)*(x) theta = (lon2-lon1)*(x) c = math.acos((math.cos(a)*math.cos(b)) + (math.sin(a)*math.sin(b)*math.cos(theta))) return radius*c

#p1_test.py import timeit lon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826 num = 500000 t = timeit.Timer("p1.great_circle(%f,%f,%f,%f)" % (lon1,lat1,lon2,lat2), "import p1") print "Pure python function", t.timeit(num), "sec"

# 测试结果 Pure python function 2.25580382347 sec

Cython: 使用Python的math模块


#c1.pyx import math def great_circle(float lon1,float lat1,float lon2,float lat2): cdef float radius = 3956.0 cdef float pi = 3.14159265 cdef float x = pi/180.0 cdef float a,b,theta,c a = (90.0-lat1)*(x) b = (90.0-lat2)*(x) theta = (lon2-lon1)*(x) c = math.acos((math.cos(a)*math.cos(b)) + (math.sin(a)*math.sin(b)*math.cos(theta))) return radius*c

# setup.py from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext ext_modules=[ Extension("c1", ["c1.pyx"]) ] setup( name = "Demos", cmdclass = {"build_ext": build_ext}, ext_modules = ext_modules )

python setup.py build_ext --inplace

#c1_test.py import timeit lon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826 num = 500000 t = timeit.Timer("c1.great_circle(%f,%f,%f,%f)" % (lon1,lat1,lon2,lat2), "import c1") print "Pure python function", t.timeit(num), "sec"

#执行结果: Pure python function 1.87078690529 sec

Cython:使用C的math库


#c2.pyx cdef extern from "math.h": float cosf(float theta) float sinf(float theta) float acosf(float theta) def great_circle(float lon1,float lat1,float lon2,float lat2): cdef float radius = 3956.0 cdef float pi = 3.14159265 cdef float x = pi/180.0 cdef float a,b,theta,c a = (90.0-lat1)*(x) b = (90.0-lat2)*(x) theta = (lon2-lon1)*(x) c = acosf((cosf(a)*cosf(b)) + (sinf(a)*sinf(b)*cosf(theta))) return radius*c

#setup.py from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext ext_modules=[ Extension("c2", ["c2.pyx"], libraries=["m"]) # Unix-like specific ] setup( name = "Demos", cmdclass = {"build_ext": build_ext}, ext_modules = ext_modules )

python setup.py build_ext --inplace

# c2_test.py import timeit lon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826 num = 500000 t = timeit.Timer("c2.great_circle(%f,%f,%f,%f)" % (lon1,lat1,lon2,lat2), "import c2") print "Pure python function", t.timeit(num), "sec"

#执行结果 Pure python function 0.34069108963 sec


Cython:使用C函数


#c3.pyx cdef extern from "math.h": float cosf(float theta) float sinf(float theta) float acosf(float theta) cdef float _great_circle(float lon1,float lat1,float lon2,float lat2): cdef float radius = 3956.0 cdef float pi = 3.14159265 cdef float x = pi/180.0 cdef float a,b,theta,c a = (90.0-lat1)*(x) b = (90.0-lat2)*(x) theta = (lon2-lon1)*(x) c = acosf((cosf(a)*cosf(b)) + (sinf(a)*sinf(b)*cosf(theta))) return radius*c def great_circle(float lon1,float lat1,float lon2,float lat2,int num): cdef int i cdef float x for i from 0 <= i < num: x = _great_circle(lon1,lat1,lon2,lat2) return x

#setup.py from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext ext_modules=[ Extension("c3", ["c3.pyx"], libraries=["m"]) # Unix-like specific ] setup( name = "Demos", cmdclass = {"build_ext": build_ext}, ext_modules = ext_modules )

python setup.py build_ext --inplace

#c3_test.py import timeit lon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826 num = 500000 t = timeit.Timer("c2.great_circle(%f,%f,%f,%f)" % (lon1,lat1,lon2,lat2), "import c2") print "Pure python function", t.timeit(num), "sec"

#测试结果 Pure python function 0.340164899826 sec

测试结论


# python代码 Pure python function 2.25580382347 sec # Cython,使用Python的math模块 Pure python function 1.87078690529 sec # Cython,使用C的math库 Pure python function 0.34069108963 sec # Cython,使用纯粹的C函数 Pure python function 0.340164899826 sec

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值