Convert string to command..

Abandoned besturk at gmail.com
Thu Oct 18 11:41:30 EDT 2007


On Oct 18, 6:26 pm, Hrvoje Niksic <hnik... at xemacs.org> wrote:
> Abandoned <best... 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.  :-)


import cPickle as pickle
a="{2:3,4:6,2:7}"
s=pickle.dumps(a, -1)
g=pickle.loads(s);
print g
'{2:3,4:6,2:7}'

Thank you very much for your answer but result is a string ??




More information about the Python-list mailing list