为什么pickle加载numpy数组比numpy快得多?

时间:2019-02-18 22:09:24

标签: python numpy pickle

我在发布问题之前已经检查过Why does pickle take so much longer than np.save?。 从那里的答案中,我们可以认为numpyndarrays一起使用应该更快。
但是看看这些实验!

我们测试的功能:

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)

为什么会这样?

0 个答案:

没有答案