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

 

 

你可能感兴趣的:(python,function,Build,扩展,import,float)