Skip to content

Instantly share code, notes, and snippets.

@albop
Created February 1, 2016 15:48
Show Gist options
  • Save albop/879e08372054ce6ce606 to your computer and use it in GitHub Desktop.
Save albop/879e08372054ce6ce606 to your computer and use it in GitHub Desktop.
from cffi import FFI
ffi = FFI()
ffi.cdef('double erfc(double x);')
libm = ffi.dlopen("m")
erfc = libm.erfc
from math import erfc as p_erfc
from numba import njit
from numpy import linspace
@njit
def test_loop_libm(x):
t = 0.0
for i in range(x.shape[0]):
e = x[i]
t += erfc(e)
return t
@njit
def test_loop_cpython(x):
t = 0.0
for i in range(x.shape[0]):
e = x[i]
t += p_erfc(e)
return t
x = linspace(0.5,1.0,100000)
assert( abs(test_loop_cpython(x) - test_loop_libm(x))<1e-8 )
import time
t1 = time.time()
for i in range(100):
test_loop_cpython(x)
t2 = time.time()
for i in range(100):
test_loop_libm(x)
t3 = time.time()
print('cpython: {}'.format(t2-t1))
print('libm: {}'.format(t3-t2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment