reduce()--what is it good for? (was: Re: reduce() anomaly?)
Alex Martelli
aleaxit at yahoo.com
Wed Nov 5 18:13:53 EST 2003
More information about the Python-list mailing list
Wed Nov 5 18:13:53 EST 2003
- Previous message (by thread): reduce()--what is it good for? (was: Re: reduce() anomaly?)
- Next message (by thread): reduce()--what is it good for? (was: Re: reduce() anomaly?)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Erik Max Francis wrote: ... > Francis Avila wrote: > >> Just out of curiosity, for what kind of problems do we find reduce to >> just >> be the Right Way? I mean, summing a big list of numbers is fun and >> all, but >> I never find any use for it otherwise. I *often* try to think if ... > I don't use reduce extremely routinely, but I certainly do find myself > using it. Grepping through my Python sources, the most common uses I > find are summing together lists of numeric values and joining together In Python 2.3, we have sum for that (much faster, too). > (flattening) a list of lists (though only when the contained lists > themselves do not contain any sequences as elements). _UN_fortunately sums works for that too -- but almost as slowly as reduce. E.g., on a small example: [alex at lancelot bo]$ timeit.py -c -s'lol=[range(20)]*20' 'reduce(list.__add__, lol, [])' 10000 loops, best of 3: 91 usec per loop [alex at lancelot bo]$ timeit.py -c -s'lol=[range(20)]*20' -s'import operator' 'reduce(operator.add, lol, [])' 10000 loops, best of 3: 88 usec per loop [alex at lancelot bo]$ timeit.py -c -s'lol=[range(20)]*20' 'sum(lol, [])' 10000 loops, best of 3: 82 usec per loop while a simple loop is way faster: [alex at lancelot bo]$ timeit.py -c -s'lol=[range(20)]*20' 'x=[]' 'for l in lol: x.extend(l)' 10000 loops, best of 3: 26 usec per loop and can easily be sped up a little more: [alex at lancelot bo]$ timeit.py -c -s'lol=[range(20)]*20' 'x=[]' 'xe=x.extend' 'for l in lol: xe(l)' 10000 loops, best of 3: 20 usec per loop Given the typical use cases of reduce are covered by sum -- and sometimes even better by simple loops &c -- then I would say that in Python 2.3 and following reduce should not be used often at all. Alex
- Previous message (by thread): reduce()--what is it good for? (was: Re: reduce() anomaly?)
- Next message (by thread): reduce()--what is it good for? (was: Re: reduce() anomaly?)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list