Fatest standard way to sum bytes (and their squares)?

Erik Max Francis max at alcyone.com
Sun Aug 12 22:28:19 EDT 2007


Peter Otten wrote:

> from itertools import imap
> 
> def summit_array(data=data, lookup=lookup):
>     a = array.array("B")
>     a.fromstring(data)
>     return sum(a), sum(imap(lookup.__getitem__, a))
> 
> $ python -m timeit -s'from summit import summit_array as summit' 'summit()'
> 100 loops, best of 3: 9.15 msec per loop
> 
> I think I'll stop here...

Yes, this was the fastest approach of all the alternatives I tried. 
Plugging the algorithm in the tool, it makes it about twice as fast as 
the generator-sum method I was using before, and about _five_ time as 
fast as a naive for loop.  The use of an array is particularly clever; I 
never would have thought of that.

Thanks to everyone for their ideas.  Amusingly I posted it late last 
night (well, this morning I guess), not expecting much, at least until 
the next day, but got a factor of two speedup within half an hour!

-- 
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
   It is morale that wins the victory.
    -- Gen. George C. Marshall



More information about the Python-list mailing list