编写一个装饰器@performance,打印出函数调用的时间。

import time

def performance(f):
    def g(*args,**kw):
        r1=time.time()
        r=f(*args,**kw)
        r2=time.time()
        print 'call %s() in %f'%(f.__name__,r2-r1)
        return r
    return g

@performance
def factorial(n):
    return reduce(lambda x,y: x*y, range(1, n+1))

print factorial(10)

posted on 2019-04-09 21:51  小二妮儿  阅读(74)  评论(0编辑  收藏  举报