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]])
也就是说,逗号似乎也有速度优势:
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
2条答案
按热度按时间rekjcdws1#
几乎总是使用逗号,这不是出于性能原因,而是因为索引两次并不完全等效:
也就是说,逗号似乎也有速度优势:
我不知道跑得最慢和跑得最快有这么大的时差是怎么回事。
fkaflof62#
第二种情况效率较低,因为新的临时数组是在第一个索引之后创建的,该索引随后被索引。