Steven D'Aprano wrote:

If you want to see reduce really shine, time it with a C-based function rather than one written in pure Python:

Timer('reduce(add, xrange(10000))',
... 'from operator import add').repeat(number=10000)
[19.724750995635986, 19.410486936569214, 19.614511013031006]
Timer('reduce(add, xrange(10000))',
... 'def add(x, y): return x+y').repeat(number=10000)
[45.210143089294434, 44.814558982849121, 46.906874895095825]
...
Of course, sum() is even faster than reduce:

Timer('sum(xrange(10000))').repeat(number=10000)
[9.814924955368042, 8.7169640064239502, 9.5062401294708252]

'Of course', because the irreducible difference between reduce(add.seq) and sum(seq) is that reduce has to call an add function while sum has the operation built-in in place of a call.

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to