我试图更好地理解 numpy 的 memmap 如何处理非常大文件的视图。下面的脚本打开一个内存映射的 2048^3 数组,并复制它的下采样 128^3 视图
import numpy as np
from time import time
FILE = '/Volumes/BlackBox/test.dat'
array = np.memmap(FILE, mode='r', shape=(2048,2048,2048), dtype=np.float64)
t = time()
for i in range(5):
view = np.array(array[::16, ::16, ::16])
t = ((time() - t) / 5) * 1000
print "Time (ms): %i" % t
通常,这会打印Time (ms): 80
左右。但是,如果我将视图分配更改为
view = np.array(array[1::16, 2::16, 3::16])
并运行三遍,我得到以下信息:
Time (ms): 9988
Time (ms): 79
Time (ms): 78
有谁明白为什么第一次调用这么慢?