What's the time complexity of indexing a numpy array directly

前端 未结 3 969
情话喂你
情话喂你 2021-01-02 07:06

I assume when having a numpy array, let\'s say

>>>>nArray
array([[  23425.     ,  521331.40625],
       [  23465.     ,  521246.03125],
       [         


        
3条回答
  •  情深已故
    2021-01-02 07:42

    On the one hand

    must be using a hash-table which will give a time complexity close to O(1). Is that right?

    is not quite true. Numpy arrays are basically contiguous blocks of homogeneous memory, with some extra info on the side on dimensions and such. Therefore, the access is O(1), and just involves some trivial math to determine the position within the memory.

    On the other hand

    indexing must be pretty efficient.

    is unfortunately not true at all. Asides from bounds checking (which arrays do), everything involving pure python is extremely inefficient (and accesses involve pure-python calls). Numpy array access is no exception. You should try to use vector operations whenever possible.

提交回复
热议问题