Question regarding a recent article on informit.com

Robin Becker robin at jessikat.fsnet.co.uk
Thu Mar 20 13:15:58 EST 2003


In article <TLkea.125664$zo2.3293141 at news2.tin.it>, Alex Martelli <aleax at aleax.it> writes
>Peter Hansen wrote:
.....
>ilter.
>
>Absolutely right.  More often than not, the right replacement for
>reduce is just a simple loop.  Consider...:
>
>$ python timeit.py 'reduce(lambda x, y: x+y, range(100))'
>10000 loops, time: 81.392 usec
>$ python timeit.py -s 'import operator' 'reduce(operator.add, range(100))'
>10000 loops, time: 36.212 usec
>$ python timeit.py '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>timeit.py "reduce(lambda x, y: x+y, range(100))"
 10000 loops, time: 5.117

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

C:\Python\rlextra\fidelity\webeforms>timeit.py -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.
>Alex
>

-- 
Robin Becker




More information about the Python-list mailing list