2. cPickle
— A faster pickle
¶
The cPickle
module supports serialization and de-serialization of Python
objects, providing an interface and functionality nearly identical to the
pickle
module. There are several differences, the most important being
performance and subclassability.
First, cPickle
can be up to 1000 times faster than pickle
because
the former is implemented in C. Second, in the cPickle
module the
callables Pickler()
and Unpickler()
are functions, not classes.
This means that you cannot use them to derive custom pickling and unpickling
subclasses. Most applications have no need for this functionality and should
benefit from the greatly improved performance of the cPickle
module.
The pickle data stream produced by pickle
and cPickle
are
identical, so it is possible to use pickle
and cPickle
interchangeably with existing pickles. [1]
There are additional minor differences in API between cPickle
and
pickle
, however for most applications, they are interchangeable. More
documentation is provided in the pickle
module documentation, which
includes a list of the documented differences.
Footnotes
[1] | Since the pickle data format is actually a tiny stack-oriented programming language, and some freedom is taken in the encodings of certain objects, it is possible that the two modules produce different data streams for the same input objects. However it is guaranteed that they will always be able to read each other’s data streams. |