Skip to content

Instantly share code, notes, and snippets.

@robertwb
Created October 24, 2013 04:04
Show Gist options
  • Save robertwb/7131183 to your computer and use it in GitHub Desktop.
Save robertwb/7131183 to your computer and use it in GitHub Desktop.
import time
import cython
import numpy as np
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
cdef cdiv_true(double[:] x, double[:] xi, double[:] a, double[:] b, long D):
cdef int k
cdef double numer, denom
for _ in range(100):
for k in range(D):
# numer = xi[k] - a[k]; denom = b[k] - a[k]; x[k] = numer / denom
x[k] = (xi[k] - a[k]) / (b[k] - a[k])
return x
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(False)
cdef cdiv_false(double[:] x, double[:] xi, double[:] a, double[:] b, long D):
cdef int k
cdef double numer, denom
for _ in range(100):
for k in range(D):
# numer = xi[k] - a[k]; denom = b[k] - a[k]; x[k] = numer / denom
x[k] = (xi[k] - a[k]) / (b[k] - a[k])
return x
def test_division():
D = 10000
res = np.random.rand(D)
x = np.random.rand(D)
a = np.zeros(D)
b = np.random.rand(D) + 1
tic = time.time()
cdiv_true(res, x, a, b, D)
toc = time.time()
print 'With c division: %0.08f' % (toc - tic)
tic = time.time()
cdiv_false(res, x, a, b, D)
toc = time.time()
print 'Without c division: %0.08f' % (toc - tic)
test_division()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment