I'm working on a real time data processing program in python and for the data crunching section I'm using cython.
Unfortunately initializing a numpy array using np.zeros is rather slow. I found that creating the array once at the startup and then looping through the values and setting them to zero is faster than creating a new array.
@cython.wraparound(False)
@cython.boundscheck(False)
cpdef zeros_int32(unsigned long long[::1] arr):
cdef unsigned int x
with nogil:
for x in range(arr.shape[0]):
arr[x] = 0
However I find this to be quite hacky. Is there a better way to initialize an array with all the values set to 0? perhaps using malloc and free? I know in C++ you can do something like:
uint_32t emptyArray[number_of_elements];
which creates the array with all values set to 0 but I can't seem to get it to work in cython.Once again I only care about the speed of the code so it doesn't matter if it looks messy or not very readable as long as it executes as fast as possible.
Any help would be much appreciated.