看起来好像
calloc达到了一个阈值,在该阈值下, *** 作系统会要求将内存清零,而无需手动对其进行初始化。查看源代码,
numpy.zeros最终委托来
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 an intermediate result is being cached.100000 loops, best of 3: 10 µs per loopIn [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.zeros5000x5000阵列没有初始化开销。
实际上,在您尝试访问该内存之前,该 *** 作系统甚至没有“真正”分配该内存。在无数TB可用空间的机器上,对TB级阵列的请求成功完成:
In [23]: x = np.zeros(2**40) # No MemoryError!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)