Numpy

时间:2017-06-11 19:17:55

标签: python numpy

我刚注意到zeros的{​​{1}}函数有一个奇怪的行为:

numpy

另一方面,%timeit np.zeros((1000, 1000)) 1.06 ms ± 29.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) %timeit np.zeros((5000, 5000)) 4 µs ± 66 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 似乎有正常的行为。 有人知道为什么用ones函数初始化一个小的numpy数组需要比大数组更多的时间吗?

(Python 3.5,numpy 1.11)

1 个答案:

答案 0 :(得分:12)

这看起来像calloc达到一个阈值,它会向操作系统请求内存归零,并且不需要手动初始化它。浏览源代码,numpy.zeros最终delegates to calloc获取归零内存块,如果与numpy.empty进行比较,则不执行初始化:

In [15]: %timeit np.zeros((5000, 5000))
The slowest run took 12.65 times longer than the fastest. This could mean that a
n intermediate result is being cached.
100000 loops, best of 3: 10 µs per loop

In [16]: %timeit np.empty((5000, 5000))
The slowest run took 5.05 times longer than the fastest. This could mean that an
 intermediate result is being cached.
100000 loops, best of 3: 10.3 µs per loop

您可以看到np.zeros没有5000x5000阵列的初始化开销。

事实上,操作系统甚至不是真的"分配该内存直到您尝试访问它。数TB的数组请求在没有太字节的机器上成功:

In [23]: x = np.zeros(2**40)  # No MemoryError!