Pythonic way to sum n-th list element?

Steven Taschuk staschuk at telusplanet.net
Sun Apr 20 02:31:02 EDT 2003


Quoth Alex Martelli:
  [...]
> So, I tried implementing sum in the simplest way, as a
> separate patch to operator.c (leaving add alone) -- THAT
> gives me 124 microseconds, a 50% speedup with respect to
> the simple for loop and clearly sufficient to justify
> sum's existence (together with its simplicity, clarity,
> and likely frequency of use).  [...]

+1.  Optimization of the most common case.  Excellent.

I'm not sure such a sum function belongs in the operator module,
though.  At the moment I think it might fit best as a built-in; it
goes nicely with min and max.

-- 
Steven Taschuk              Aral: "Confusion to the enemy, boy."
staschuk at telusplanet.net    Mark: "Turn-about is fair play, sir."
                             -- _Mirror Dance_, Lois McMaster Bujold





More information about the Python-list mailing list