I an using python 2.7 and trying to pickle an object. I am wondering what the real difference is between the pickle protocols.
import numpy as np
import pickle
class Data(object):
def __init__(self):
self.a = np.zeros((100, 37000, 3), dtype=np.float32)
d = Data()
print("data size: ", d.a.nbytes / 1000000.0)
print("highest protocol: ", pickle.HIGHEST_PROTOCOL)
pickle.dump(d, open("noProt", "w"))
pickle.dump(d, open("prot0", "w"), protocol=0)
pickle.dump(d, open("prot1", "w"), protocol=1)
pickle.dump(d, open("prot2", "w"), protocol=2)
out >> data size: 44.4
out >> highest protocol: 2
then I found that the saved files have different sizes on disk:
noProt
: 177.6MB prot0
: 177.6MB prot1
: 44.4MB prot2
: 44.4MBI know that prot0
is a human readable text file, so I don't want to use it.
I guess protocol 0 is the one given by default.
I wonder what's the difference between protocols 1 and 2, is there a reason why I should chose one or another?
What's is the better to use, pickle
or cPickle
?