Python numpy slice notation (comma vs index) performance difference?

Question:

Is there a difference in performance between using a comma and index references, even though both produce the same results? The latter may be more understandable for some conventional readers.

x = numpy.array([[1,2,3,4],
                 [5,6,7,8]])

comma_method = x[0,1:3] 
>>> numpy.array([2,3])

index_method = x[0][1:3]
>>> numpy.array([2,3])
Asked By: AlanSTACK

||

Answers:

Pretty much always go for the comma, not for performance reasons, but because indexing twice isn’t quite equivalent:

In [2]: x = numpy.array([[0, 1], [2, 3]])

In [3]: x[:1, :1]
Out[3]: array([[0]])

In [4]: x[:1][:1]
Out[4]: array([[0, 1]])

That said, the comma also appears to have a speed advantage:

In [7]: %timeit x[0][0]
The slowest run took 25.41 times longer than the fastest. This could mean that a
n intermediate result is being cached 
1000000 loops, best of 3: 357 ns per loop

In [8]: %timeit x[0, 0]
The slowest run took 41.92 times longer than the fastest. This could mean that a
n intermediate result is being cached 
1000000 loops, best of 3: 148 ns per loop

I’m not sure what’s up with the slowest run and the fastest run having such a time difference.

Answered By: user2357112

The second case is less efficient as a new temporary array is created after the first index that is subsequently indexed.

Answered By: Hun
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.