【问题标题】:Numpy performance gap between len(arr) and arr.shape[0]len(arr) 和 arr.shape[0] 之间的 Numpy 性能差距
【发布时间】:2018-12-22 05:51:33
【问题描述】:

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

【问题讨论】:

  • @user3483203,你确定吗?只是我没有看到任何() 这意味着代码应该只获取一个字段,我的意思是没有调用,不是吗?
  • 我相信len 的时间复杂度在 CPython 中是 O(1)。
  • 时间是ns,取决于调用细节,而不是数组的大小。为什么要重要?我很少使用len(arr),因为shape[0] 更加明确(对人眼而言)。我们必须深入研究ndarray 代码才能看到调用序列len => .__len__ =>?shape 必须编码为属性,而不是纯属性。

标签: python arrays performance numpy micro-optimization


【解决方案1】:

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有不同的ids;它们是不同的元组对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-02
    • 2014-12-23
    • 2014-03-01
    • 2019-09-08
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多