Another Cython experiment
In [1]:
%load_ext cythonmagic
In [2]:
import math
def py_great_circle(lon1, lat1, lon2, lat2):
radius = 6371 #km
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
In [3]:
%timeit py_great_circle(-72.345, 34.323, -61.823, 54.826)
In [12]:
%%cython
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 = 6371 #km
cdef float x = 3.14159/180.0
cdef float a, b, c, theta
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
In [13]:
%timeit great_circle(-72.345, 34.323, -61.823, 54.826)
In [ ]: