python - Should list item type be defined in cython? -
if send python list cython function iterate over, suppose declare type list items are? best way loop on list in cython? example:
#cython function, passed list of float items def cython_f(list example_list): cdef int in range(len(example_list)): #do stuff #but list item type not defined? pass #alternative loop cdef j float #declaration of list item type j in example_list: #do stuff pass
*edit: speed gained trying define list item type? preferable pass numpy arrays instead of python lists? apologies asking many questions.
in cython not obliged declare anything. declaring types usually helps performance. usually because if declare types, don't use them, may induce type checks , pack-unpack. way sure measure.
to declare types of list, put @ beginning cdef float value
, , in loop value = example_list[i]
.
should use list or numpy array? array uniform data container. means can declare being float32_t
, , cython know how work @ c speed (accessing faster, guaranteed contiguous , strided in memory). on other hand, if going change size, better using lists (or heavy use, perhaps libcpp.vector
). answer depends on do, in cases, array better.
to fair, have consider how data living. if have in lists, function arrays may faster, list -> array -> f_array -> array -> list
may slower list -> f_list -> list
. if don't care, rule of thumb, use arrays when length constant , lists otherwise. note numpy arrays lighter on memory big amounts of data.
Comments
Post a Comment