Convert string to command..
Hrvoje Niksic
hniksic at xemacs.org
Thu Oct 18 11:26:55 EDT 2007
Abandoned <besturk at gmail.com> writes:
> 173.000 dict elements and it tooks 2.2 seconds this very big time
> for my project
If you're generating the string from Python, use cPickle instead.
Much faster:
>>> import time
>>> d = dict((i, i+1) for i in xrange(170000))
>>> len(d)
170000
>>> s=repr(d)
>>> t0 = time.time(); d2 = eval(s); t1 = time.time()
>>> t1-t0
1.5457899570465088
>>> import cPickle as pickle
>>> s = pickle.dumps(d, -1)
>>> len(s)
1437693
>>> t0 = time.time(); d2 = pickle.loads(s); t1 = time.time()
>>> t1-t0
0.060307979583740234
>>> len(d2)
170000
That is 25x speedup. Note that cPickle's format is binary. Using the
textual format makes for more readable pickles, but reduces the
speedup to "only" 9.5x on my machine.
P.S.
Before someone says that using pickle is unsafe, remember that he is
considering *eval* as the alternative. :-)
More information about the Python-list
mailing list