Fibonacci numbers with memoization runs slow in Python?

def fib(n):
    if n == 1:
        return 0
    if n == 2:
        return 1
    return fib(n-2) + fib(n-1)


def memo(f):
    cache = {}
    def memoized(n):
        if n not in cache:
            cache[n] = f(n)
        return cache[n]
    return memoized

fib1 = memo(fib)

This code runs really slow on my laptop, but if I change the name fib1 to fib, then everything works fine...anyone know the reason ? Thanks!


fib recurses into fib , not fib1 . If the memoized version has a different name it won't get used.


In that code fib is the name of the non-memoized function. fib1 is the name you've given to the memoized function. But if you see your code, you'll see that it recursively calls fib the non-memoized version. Hence why you aren't getting a speed advantage.


I agree that adding some prints you would probably see the issue. You're very close to actually getting it.

What you have right now only stores n where n is the argument given to fib1. Inside fib, you're calling fib which won't memoize any previously computed values. So by adding a print statement to fib print "fib ", n and calling fib1(4), you will get the following output:

fib 4
fib 2
fib 3
fib 1
fib 2

So you see it calls fib with n=2 twice. The reason why fib = memo(fib) is faster is because then it's actually momoizing because you're redefining fib to be the memoized function.

链接地址: http://www.djcxy.com/p/51116.html

上一篇: Python ftplib:显示FTP上传进度

下一篇: 带有记忆的斐波纳契数字在Python中运行缓慢?