python - Why does numba have worse optimization than Cython in this code? -
i trying optimize code numba. problem simple cython optimization (just specifying data types) 6 times faster using autojit, don't know if i'm doing wrong.
the function optimize is:
from numba import autojit @autojit(nopython=true) def get_energy(system, i,j,m): #system array, (i,j) indices , m size of array up=i-1; down=i+1; left=j-1; right=j+1 if up<0: total=system[m,j] else: total=system[up,j] if down>m: total+=system[0,j] else: total+=system[down,j] if left<0: total+=system[i,m] else: total+=system[i,left] if right>m: total+=system[i,0] else: total+=system[i,right] return 2*system[i,j]*total
a simple run this:
import numpy np x=np.random.rand(50,50) get_energy(x, 3, 5, 50)
i've understood numba @ loops may not optimize other things well. anyhow, expect similar performance cython, numba slower accessing arrays or @ conditional statements?
the .pyx file in cython is:
import numpy np cimport cython cimport numpy np def get_energy(np.ndarray[np.float64_t, ndim=2] system, int i,int j,unsigned int m): cdef int cdef int down cdef int left cdef int right cdef np.float64_t total up=i-1; down=i+1; left=j-1; right=j+1 if up<0: total=system[m,j] else: total=system[up,j] if down>m: total+=system[0,j] else: total+=system[down,j] if left<0: total+=system[i,m] else: total+=system[i,left] if right>m: total+=system[i,0] else: total+=system[i,right] return 2*system[i,j]*total
please comment if need give further information.
Comments
Post a Comment