Fatest standard way to sum bytes (and their squares)?
Erik Max Francis
max at alcyone.com
Sun Aug 12 20:15:41 EDT 2007
Hrvoje Niksic wrote:
> For ordinalSum, using imap is almost twice as fast:
>
> $ python -m timeit -s 'data=[chr(x) for x in xrange(256)]' 'sum(ord(x) for x in data)'
> 10000 loops, best of 3: 92.4 usec per loop
> $ python -m timeit -s 'data=[chr(x) for x in xrange(256)]; from itertools import imap' 'sum(imap(ord, data))'
> 10000 loops, best of 3: 55.4 usec per loop
You're using data which is a list of chars (strings), rather than a
string itself, which is what the format is in. The imap optimization
doesn't appear to work quite as dramatically well for me with strings
instead of lists, but it certainly is an improvement.
> Of course, that optimization doesn't work for the squared sum; using a
> lambda only pessimizes it.
Right.
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Experience is the name everyone gives to their mistakes.
-- Oscar Wilde
More information about the Python-list
mailing list