Fatest standard way to sum bytes (and their squares)?
Duncan Booth
duncan.booth at invalid.invalid
Sun Aug 12 12:37:30 CEST 2007
Erik Max Francis <max at alcyone.com> wrote:
> So far the fastest way I've found is using the `sum` builtin and
> generators::
>
> ordinalSum = sum(ord(x) for x in data)
> ordinalSumSquared = sum(ord(x)**2 for x in data)
>
> This is about twice as fast as an explicit loop, but since it's going to
> be processing massive amounts of data, the faster the better. Are there
> any tricks I'm not thinking of, or perhaps helper functions in other
> modules that I'm not thinking of?
>
I haven't timed it, but at the very least I'd avoid going through all the
data twice:
count = {}
for i in range(256):
count[chr(i)] = 0
for x in data:
count[x] += 1
ordinalSum = sum(ord(c)*count[c] for c in count)
ordinalSumSquared = sum(ord(c)**2 * count[c] for c in count)
More information about the Python-list
mailing list