I could use some help making this Python code run faster using only Python code.
Duncan Booth
duncan.booth at invalid.invalid
Fri Sep 21 03:56:44 EDT 2007
George Sakkis <george.sakkis 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
More information about the Python-list
mailing list