Question regarding a recent article on

Robin Becker robin at
Thu Mar 20 19:15:58 CET 2003

In article <TLkea.125664$zo2.3293141 at>, Alex Martelli <aleax at> writes
>Peter Hansen wrote:
>Absolutely right.  More often than not, the right replacement for
>reduce is just a simple loop.  Consider...:
>$ python 'reduce(lambda x, y: x+y, range(100))'
>10000 loops, time: 81.392 usec
>$ python -s 'import operator' 'reduce(operator.add, range(100))'
>10000 loops, time: 36.212 usec
>$ python 'x=0' 'for y in range(100): x += y'
>10000 loops, time: 32.864 usec
>These numbers are very repeatable on my machine (a good, if dated,
>Athlon + DDR box) with Python 2.3 (built right out of CVS).
.....well on my rather slow pentium I see the following

C:\Python\rlextra\fidelity\webeforms> "reduce(lambda x, y: x+y, range(100))"
 10000 loops, time: 5.117

C:\Python\rlextra\fidelity\webeforms> -s "import operator" "reduce(operator.add,
 10000 loops, time: 3.615

C:\Python\rlextra\fidelity\webeforms> -s "x=0" "for y in range(100): x += y"
 10000 loops, time: 4.076

These are also reproducible. A 10% improvement is worth keeping surely.

Robin Becker

More information about the Python-list mailing list