|
使用Cython的性能对比
作者:liuyuan_jq
2011-01-12
python代码实现
#p1.pyimport mathdef great_circle(lon1,lat1,lon2,lat2):radius = 3956 #milesx = math.pi/180.0a = (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.pyimport timeitlon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826num = 500000t = 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.pyximport mathdef great_circle(float lon1,float lat1,float lon2,float lat2):cdef float radius = 3956.0cdef float pi = 3.14159265cdef float x = pi/180.0cdef float a,b,theta,ca = (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.pyfrom distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_extext_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.pyimport timeitlon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826num = 500000t = 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.pyxcdef 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.0cdef float pi = 3.14159265cdef float x = pi/180.0cdef float a,b,theta,ca = (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.pyfrom distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_extext_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.pyimport timeitlon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826num = 500000t = 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.pyxcdef 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.0cdef float pi = 3.14159265cdef float x = pi/180.0cdef float a,b,theta,ca = (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*cdef great_circle(float lon1,float lat1,float lon2,float lat2,int num):cdef int icdef float xfor i from 0 <= i < num:x = _great_circle(lon1,lat1,lon2,lat2)return x
#setup.pyfrom distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Distutils import build_extext_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.pyimport timeitlon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826num = 500000t = 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 |
|