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

Numpy performance gap between len(arr) and arr.shape[0]

我发现 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 数组会产生什么开销?)

numpy数组数据结构是用C语言实现的。数组的维度存储在C结构中。它们不存储在 Python 元组中。因此,每次读取 shape 属性时,都会创建新的 Python 整数对象的新 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有不同的id;它们是不同的元组对象。