I could use some help making this Python code run faster using only Python code.
Python Maniac
raychorn at hotmail.com
Sat Sep 22 16:02:51 EDT 2007
On Sep 21, 12:56 am, Duncan Booth <duncan.bo... at invalid.invalid>
wrote:
> George Sakkis <george.sak... at gmail.com> wrote:
> > It has to do with the input string length; try multiplying it by 10 or
> > 100. Below is a more complete benchmark; for largish strings, the imap
> > version is the fastest among those using the original algorithm. Of
> > course using a lookup table as Diez showed is even faster. FWIW, here
> > are some timings (Python 2.5, WinXP):
>
> > scramble: 1.818
> > scramble_listcomp: 1.492
> > scramble_gencomp: 1.535
> > scramble_map: 1.377
> > scramble_imap: 1.332
> > scramble_dict: 0.817
> > scramble_dict_map: 0.419
> > scramble_dict_imap: 0.410
>
> I added another one:
>
> import string
> scramble_translation = string.maketrans(''.join(chr(i) for i in xrange
> (256)), ''.join(chr(i|0x80) for i in xrange(256)))
> def scramble_translate(line):
> return string.translate(line, scramble_translation)
>
> ...
> funcs = [scramble, scramble_listcomp, scramble_gencomp,
> scramble_map, scramble_imap,
> scramble_dict, scramble_dict_map, scramble_dict_imap,
> scramble_translate
> ]
>
> and I think I win:
>
> scramble: 1.949
> scramble_listcomp: 1.439
> scramble_gencomp: 1.455
> scramble_map: 1.470
> scramble_imap: 1.546
> scramble_dict: 0.914
> scramble_dict_map: 0.415
> scramble_dict_imap: 0.416
> scramble_translate: 0.007
Some benchmarks showing the effectiveness of using Psyco:
scramble: 4.210
scramble_listcomp: 2.343
scramble_gencomp: 2.599
scramble_map: 1.960
scramble_imap: 2.231
scramble_dict: 2.387
scramble_dict_map: 0.535
scramble_dict_imap: 0.726
scramble_translate: 0.010
Now with Psyco...
psyco.bind(scramble)...
scramble: 0.121 4.088 34.670x faster
scramble_listcomp: 0.215 2.128 10.919x faster
scramble_gencomp: 2.563 0.036 1.014x faster
scramble_map: 2.002 -0.043 0.979x slower
scramble_imap: 2.175 0.056 1.026x faster
scramble_dict: 0.199 2.188 11.983x faster
scramble_dict_map: 0.505 0.029 1.058x faster
scramble_dict_imap: 0.728 -0.001 0.998x slower
scramble_translate: 0.009 0.001 1.111x faster
Overall, Psyco helped my little process of converting 20 MB worth of
ASCII into what may not look like the original at 6x faster than
without Psyco.
More information about the Python-list
mailing list