Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python2: Should I use Pickle or cPickle?

Python 2 has both the pickle and cPickle modules for serialization.

cPickle has an obvious advantage over pickle: speed. What, if any, advantage does pickle have over cPickle?

like image 974
user200783 Avatar asked May 09 '16 09:05

user200783


People also ask

Why pickle is not good in Python?

Pickle on the other hand is slow, insecure, and can be only parsed in Python. The only real advantage to pickle is that it can serialize arbitrary Python objects, whereas both JSON and MessagePack have limits on the type of data they can write out.

What kind of information is best stored using the cPickle module?

cPickle supports most elementary data types (e.g., dictionaries, lists, tuples, numbers, strings) and combinations thereof, as well as classes and instances. Pickling classes and instances saves only the data involved, not the code.

What is cPickle Python?

In Python 2, cPickle is the accelerated version of pickle, and a later addition to the standard library. It was perfectly normal to import it with a fallback to pickle. In Python 3 the accelerated version has been integrated and there is simply no reason to use anything other than import pickle .

What can I use instead of a pickle in Python?

An alternative is cPickle. It is nearly identical to pickle , but written in C, which makes it up to 1000 times faster. For small files, however, you won't notice the difference in speed. Both produce the same data streams, which means that Pickle and cPickle can use the same files.


2 Answers

The pickle module implements an algorithm for turning an arbitrary Python object into a series of bytes. This process is also called serializing” the object. The byte stream representing the object can then be transmitted or stored, and later reconstructed to create a new object with the same characteristics.

The cPickle module implements the same algorithm, in C instead of Python. It is many times faster than the Python implementation, but does not allow the user to subclass from Pickle. If subclassing is not important for your use, you probably want to use cPickle.

Source of above information.

like image 198
rkatkam Avatar answered Sep 24 '22 23:09

rkatkam


I found this regarding pickle and cPickle:

"The pickle module implements an algorithm for turning an arbitrary Python object into a series of bytes....

The cPickle module implements the same algorithm, in C instead of Python. It is many times faster than the Python implementation, but does not allow the user to subclass from Pickle.

If subclassing is not important for your use, you probably want to use cPickle."

Source: https://pymotw.com/2/pickle/

like image 21
Charlton Lane Avatar answered Sep 25 '22 23:09

Charlton Lane