In order to get fast division in cython, I can use the compiler directive
@cython.cdivision(True)
This works, in that the resulting c code has no zero division checking. However for some reason it is actually making my code slower. Here is an example:
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
@cython.cdivision(True)
def example1(double[:] xi, double[:] a, double[:] b, int D):
cdef int k
cdef double[:] x = np.zeros(D)
for k in range(D):
x[k] = (xi[k] - a[k]) / (b[k] - a[k])
return x
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def example2(double[:] xi, double[:] a, double[:] b, int D):
cdef int k
cdef double[:] x = np.zeros(D)
for k in range(D):
x[k] = (xi[k] - a[k]) / (b[k] - a[k])
return x
def test_division(self):
D = 10000
x = np.random.rand(D)
a = np.zeros(D)
b = np.random.rand(D) + 1
tic = time.time()
example1(x, a, b, D)
toc = time.time()
print 'With c division: ' + str(toc - tic)
tic = time.time()
example2(x, a, b, D)
toc = time.time()
print 'Without c division: ' + str(toc - tic)
This results in output:
With c division: 0.000194787979126
Without c division: 0.000176906585693
Is there any reason why turning off zero division checking could slow down things (I know there are no zero divisors).