我在发布问题之前已经检查过Why does pickle take so much longer than np.save?。
从那里的答案中,我们可以认为numpy
与ndarrays
一起使用应该更快。
但是看看这些实验!
我们测试的功能:
import numpy as np
import pickle as pkl
a = np.random.randn(1000,5)
with open("test.npy", "wb") as f:
np.save(f, a)
with open("test.pkl", "wb") as f:
pkl.dump(a,f)
def load_with_numpy(name):
for i in range(1000):
with open(name, "rb") as f:
np.load(f)
def load_with_pickle(name):
for i in range(1000):
with open(name, "rb") as f:
pkl.load(f)
实验结果:
%timeit load_with_numpy("test.npy")
296 ms ± 1.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit load_with_pickle("test.pkl")
28.2 ms ± 994 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
为什么会这样?