Discuss / Python / 函数运行时长装饰器作业

函数运行时长装饰器作业

Topic source

极大似然

#1 Created at ... [Delete] [Delete and Lock User]

def metric(fn):

    def wrapper(*args, **kw):

        t1 = time.time() #执行前时刻

        r_val = fn(*args, **kw) #执行函数,同时接受返回值

        t2 = (time.time() - t1) * 1000 #执行时长

        print('%s executed in %.2f ms' % (fn.__name__, t2))

        return r_val #引入变量,防止功能性函数(如打印)执行两遍

    return wrapper


  • 1

Reply