len(arr)和arr.shape [0]之间的Numpy性能差距

问题描述 投票:6回答:1

我发现len(arr)几乎是arr.shape[0]的两倍,我想知道为什么。

我使用的是Python 3.5.2,Numpy 1.14.2,IPython 6.3.1

以下代码演示了这一点:

arr = np.random.randint(1, 11, size=(3, 4, 5))

%timeit len(arr)
# 62.6 ns ± 0.239 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

%timeit arr.shape[0]
# 102 ns ± 0.163 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

我还做了一些比较测试:

class Foo():
    def __init__(self):
        self.shape = (3, 4, 5)        

foo = Foo()

%timeit arr.shape
# 75.6 ns ± 0.107 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

%timeit foo.shape
# 61.2 ns ± 0.281 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

%timeit foo.shape[0]
# 78.6 ns ± 1.03 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

所以我有两个问题:

1)为什么len(arr)arr.shape[0]工作得更快? (我原本以为len会因为函数调用而变慢)

2)为什么foo.shape[0]arr.shape[0]工作得更快? (换句话说,在这种情况下,numpy数组会产生什么开销呢?)

python arrays performance numpy micro-optimization
1个回答
6
投票

numpy数组数据结构用C实现。数组的维度存储在C结构中。它们不存储在Python元组中。因此,每次读取shape属性时,都会创建一个新的Python整数对象元组。当你使用arr.shape[0]时,那个元组会被索引以拉出第一个元素,这会增加更多的开销。 len(arr)只需要创建一个Python整数。

您可以轻松验证arr.shape每次读取时都会创建一个新元组:

In [126]: arr = np.random.randint(1, 11, size=(3, 4, 5))

In [127]: s1 = arr.shape

In [128]: id(s1)
Out[128]: 4916019848

In [129]: s2 = arr.shape

In [130]: id(s2)
Out[130]: 4909905024

s1s2有不同的ids;它们是不同的元组对象。

© www.soinside.com 2019 - 2024. All rights reserved.