map/filter/reduce/lambda opinions and background unscientific mini-survey

Steven Bethard steven.bethard at gmail.com
Mon Jul 4 14:16:47 EDT 2005


Erik Max Francis wrote:
> Ron Adam wrote:
> 
>> In this case sum and product fulfill 90% (estimate of course) of 
>> reduces use cases.  It may actually be as high as 99% for all I know. 
>> Or it may be less.  Anyone care to try and put a real measurement on it?
> 
> Well, reduce covers 100% of them, and it's one function, and it's 
> already there.

And it's almost two times slower:

$ python -m timeit -s "x = xrange(1000)" "sum(x)"
10000 loops, best of 3: 92.5 usec per loop

$ python -m timeit -s "from operator import add; x = xrange(1000)" 
"reduce(add, x)"
10000 loops, best of 3: 157 usec per loop

And that's only if I have the sense to import from operator:

$ python -m timeit -s "x = xrange(1000); add = lambda x, y: x + y" 
"reduce(add, x)"
1000 loops, best of 3: 587 usec per loop

Note that the simple for-loop beats the case where you define your own 
function because it doesn't have the repeated overhead of function calls 
(which are expensive in Python):

$ python -m timeit -s "sum = 0; x = xrange(1000)" "for i in x: sum += i"
10000 loops, best of 3: 291 usec per loop

What would really help here is if you could identify the cases where you 
think reduce is really a gain.  A lot of them are actually not good 
practice in Python because of the function-call overhead.  However, I'm 
willing to be convinced otherwise with a few good examples.

STeVe



More information about the Python-list mailing list