reduce()--what is it good for?

JCM joshway_without_spam at
Fri Nov 7 18:20:18 CET 2003

Alex Martelli <aleax at> wrote:
...various good points...

> if one is in a hurry, recursion and
> memoization are obviously preferable:

> def facto(n, _memo={1:1}):
>     try: return _memo[n]
>     except KeyError:
>         result = _memo[n] = (n-1) * facto(n-1)
>         return result

> the performance numbers being:

> [alex at lancelot bo]$ -c -s'import facs' 'facs.factorial(13)'
> 100000 loops, best of 3: 10.3 usec per loop

> [alex at lancelot bo]$ -c -s'import facs' 'facs.fac(13)'
> 10000 loops, best of 3: 32 usec per loop

> [alex at lancelot bo]$ -c -s'import facs' 'facs.facto(13)'
> 1000000 loops, best of 3: 1.26 usec per loop

I'm going off topic, but it's really not fair to compare a memoized
function to non-memoized implementations using a "best of 3" timing

More information about the Python-list mailing list