Python:使用decorator计算递归函数的执行时间

Fen*_*ong 1 python recursion decorator

我想编写一个行为与给定函数完全相似的函数,只不过它会打印执行该函数所消耗的时间。像这样:

>>> fib = profile(fib)
>>> fib(20)
time taken: 0.1 sec
10946
Run Code Online (Sandbox Code Playgroud)

这是我的代码,它将在每个函数调用中打印消息。

import time


def profile(f):
    def g(x):
        start_time = time.clock()
        value = f(x)
        end_time = time.clock()
        print('time taken: {time}'.format(time=end_time-start_time))
        return value
    return g


@profile
def fib(n):
    if n is 0 or n is 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)
Run Code Online (Sandbox Code Playgroud)

我上面的代码将为每个fib(n-1)打印一条消息``耗时:...'',因此会有很多消息``耗时:...''。我可以找到一种方法来仅打印fib(20)的执行时间,而不是每个fib(n-1)的执行时间吗?

Kev*_*vin 5

我假设您的问题是“我该怎么写,profile以便即使我装饰了一个可以调用自身的函数,也只打印一条消息?”

您可以跟踪当前正在计时的功能。这样,如果一个函数调用了它自己,您就知道您已经对它进行了进一步的计时,并且不需要为第二个实例做任何事情。

def profile(f, currently_evaluating=set()):
    #`currently_evaluating` will persist across all decorated functions, due to "mutable default argument" behavior.
    #see also http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument
    def g(x):
        #don't bother timing it if we're already doing so
        if f in currently_evaluating: 
            return f(x)
        else:
            start_time = time.clock()
            currently_evaluating.add(f)
            try:
                value = f(x)
            finally:
                currently_evaluating.remove(f)
            end_time = time.clock()
            print('time taken: {time}'.format(time=end_time-start_time))
            return value
    return g
Run Code Online (Sandbox Code Playgroud)

如果您使用的是3.X,则可以通过使用nonlocal关键字来减少可变的默认参数怪异性。

def profile(f):
    is_evaluating = False
    def g(x):
        nonlocal is_evaluating
        if is_evaluating:
            return f(x)
        else:
            start_time = time.clock()
            is_evaluating = True
            try:
                value = f(x)
            finally:
                is_evaluating = False
            end_time = time.clock()
            print('time taken: {time}'.format(time=end_time-start_time))
            return value
    return g
Run Code Online (Sandbox Code Playgroud)