Is a "real" C-Python possible?
sturlamolden
sturlamolden at yahoo.no
Tue Dec 11 11:05:09 EST 2007
On 11 Des, 10:10, Duncan Booth <duncan.bo... at invalid.invalid> 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.
More information about the Python-list
mailing list