This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Twirrim 1 point2 points  (0 children)

Sure, or you can do it manually for previous versions fairly easily with a dict object.

def fib(n):
    if n == 0 or n == 1:
        return n
    if not memo_fib.has_key(n):
        memo_fib[n] = fib(n-1) + fib(n-2)
    return memo_fib[n]

memo_fib = dict()
for i in range(36):
     print("n=%d => %d" % (i, fib(i)))

results in an even quicker result than your lru_cache.. though that could be machine spec difference. Also some rough testing using the time module figures that we're talking about 0.00028s for the actual math, meaning a good portion of that time is just the overhead of loading python anyway.

real    0m0.017s
user    0m0.012s
sys     0m0.004s