Created
February 1, 2016 15:48
-
-
Save albop/879e08372054ce6ce606 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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