I am learning cython. I wrote a numpy code:
from numpy import *
def set_onsite(n):
a=linspace(0,n,n+1)
onsite=zeros([n+1,n+1],float)
for i in range(0,n+1):
onsite[i,i]=a[i]*a[i]
return onsite
and I used %timeit to calculate the time
%timeit ex5.set_onsite(10000)
10 loops, best of 3: 24.2 ms per loop
So I wrote a cython version like this:
import numpy as np
cimport numpy as np
cimport cython
import cython
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def set_onsite(np.int_t n):
cdef np.ndarray[double,ndim=1,mode='c'] a=np.linspace(0,n,n+1)
cdef np.ndarray[double,ndim=2,mode='c'] onsite=np.empty(n+1,n+1)
cdef np.int_t i
for i in range(0,n+1):
onsite[i,i]=a[i]*a[i]
return onsite
and this time the result is:
%timeit ex5.set_onsite(10000)
100 loops, best of 3: 18.1 ms per loop
the results are almost the same. I do not satisfy with this result, so I was wondering is there any way to make the code faster than 18.1 ms per loop?
numpyfunctions,linspace,empty,rangeand indexing. To get real speed you need usenditerto iterate, including generatinga, and the initializingonsitearray.onsitewithempty, so the off diagonal values can be anything.