编写一个装饰器@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)