Convert string to command..
hniksic at xemacs.org
Fri Oct 19 15:02:43 CEST 2007
Hrvoje Niksic <hniksic at xemacs.org> 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
>>>> t0 = time.time(); d2 = pickle.loads(s); t1 = time.time(); t1-t0
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
>>> 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