I could use some help making this Python code run faster using only Python code.

Python Maniac raychorn at hotmail.com
Fri Sep 21 19:00:11 CEST 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

Wow !

Now I am very impressed with Python !

The difference between where I began (70.155 secs) and where we end
(2.278 secs) is a whopping 30.8x faster using some rather simple
techniques that are nothing more than variations on the theme of
hoisting function calls out of loops along with using some very
powerful iterator functions from Python.

My best runtime with Ruby using the same machine and OS was 67.797
secs which is 29.8x slower than the fastest Python runtime.  This
makes Ruby almost as slow as Python was made faster.  The irony with
Ruby was that the use of a hash in Ruby actually made the Ruby code
run slower than when a hash was not used.

Now I think I will code this little scrambler using nothing but the D
Language just to see whether there is any benefit in using D over
Python for this sort of problem.




More information about the Python-list mailing list