Convert string to command..

Hrvoje Niksic hniksic at
Fri Oct 19 15:02:43 CEST 2007

Hrvoje Niksic <hniksic at> writes:

> If you're generating the string from Python, use cPickle instead.
> Much faster:
>>>> t0 = time.time(); d2 = eval(s); t1 = time.time(); t1-t0
> 1.5457899570465088
>>>> t0 = time.time(); d2 = pickle.loads(s); t1 = time.time(); t1-t0
> 0.060307979583740234

It just occurred to me, for simple data structures like the ones we're
discussing here (dicts of ints), marshal should also be considered.
marshal is the module used for generating and loading .pyc files and,
while it doesn't support all the bells and whistles of pickle, it's
very fast:

>>> t0 = time.time(); d2 = marshal.loads(s); t1 = time.time(); t1-t0

Benchmarks made with the timeit module confirm this difference.

Marshal has the added advantage of using much less space than the
(binary) pickle -- the example dictionary provided above pickled to a
string of 2667791 bytes, while marshal produced a string of 1700002

More information about the Python-list mailing list