Reputation: 6065
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])
Upvotes: 1
Views: 469
Reputation: 3847
The second case is less efficient as a new temporary array is created after the first index that is subsequently indexed.
Upvotes: 0
Reputation: 281177
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.
Upvotes: 3