Python numpy切片表示法(逗号与索引)性能差异?

pxiryf3j  于 2023-02-02  发布在  Python
关注(0)|答案(2)|浏览(83)

使用逗号和索引引用之间是否有性能上的差异,即使两者产生相同的结果?后者对于一些传统的读者来说可能更容易理解。

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])
rekjcdws

rekjcdws1#

几乎总是使用逗号,这不是出于性能原因,而是因为索引两次并不完全等效:

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

我不知道跑得最慢和跑得最快有这么大的时差是怎么回事。

fkaflof6

fkaflof62#

第二种情况效率较低,因为新的临时数组是在第一个索引之后创建的,该索引随后被索引。

相关问题