On 11 Des, 10:10, Duncan Booth <[EMAIL PROTECTED]> wrote: > @memoize(3) > def fib(n): > if n == 0 or n == 1: > return n > else: > return fib(n-1) + fib(n-2)
The thing I would do is: def fibo(n): while 1: try: return fibo.seq[n] except AttributeError: fibo.seq = [0, 1, 1] except IndexError: fibo.seq.append( fibo.seq[-2] + fibo.seq[-1] ) Here are some timings I got on my laptop (1.7 GHz Pentium M, Windows XP, Python 2.5.1), calculating 36 Fibonacci numbers: First run, initalizing cache: 243 µs Second run, exploiting cache: 28 µs Third run, exploting cache: 27 µs This is 6 orders of magnitude faster than Congiano's benchmark. That is a speed up by a factor of a million. -- http://mail.python.org/mailman/listinfo/python-list