Cython actually slowing me down

早过忘川 提交于 2019-12-25 08:29:57

问题


I am trying to 'cythonize' my code however while the following code does work, it is not adding any speed to my code (in fact its a shade slower). I am wondering if anyone knows what I am doing wrong if anything at all. Note I am passing a numpy array and its data type should be float16. Never used cython before and I am using Jupyter notebook right now. In the cell above I have Cython loaded.

%%cython
import numpy as np
cimport numpy as np

DTYPE = np.float16
ctypedef np.int_t DTYPE_t

def arrange_waveforms(np.ndarray arr,dim,mintomaxEventslistrange):
    import timeit
    start_time = timeit.default_timer()
    cdef int key
    #mintomaxEventslistrange is basically range(2000000)
    dictlist = dict((key,  [[] for _ in xrange(1536)]) for key in mintomaxEventslistrange)      
# A seperate one to hold the timing info (did this to minimize memory of dictlist)
    window = dict((key,  [[] for _ in xrange(1536)]) for key in mintomaxEventslistrange)
#arrange waveforms
    cdef np.ndarray pixel=arr[:,0].astype(int)
    cdef int i
    cdef int lines = dim[0]
    for i in range(lines):

        dictlist[arr[i,1]][pixel[i]].extend(arr[i,9:])
        window[arr[i,1]][pixel[i]].append(arr[i,6]) 
    elapsed = timeit.default_timer() - start_time
    print elapsed
    return dictlist,window

来源:https://stackoverflow.com/questions/40233664/cython-actually-slowing-me-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!