求问大神,调试问题。为什么我代码跑起来显示的是4ms,而第一名同学跑的是0.03ms
#coding=utf8
import time
def performance(unit):
def fn(f):
def print_time(*args, **kw):
t1 = time.time()
r= f(*args,**kw)
t2 = time.time()
t = (t2 - t1)*1000 if unit =='ms' else (t2 - t1) #如果是ms,则*1000,如果不是则直接得到t2-t1
print 'call %s() in %f %s' % ( f.__name__ , t, unit)
return r
return print_time
return fn
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(unit):
def perf_decorator(f):
def wrapper(*args, **kw):
t1 = time.time()
r = f(*args, **kw)
t2 = time.time()
t = (t2 - t1)*1000 if unit =='ms' else (t2 - t1)
print 'call %s() in %f %s'%(f.__name__, t, unit)
return r
return wrapper
return perf_decorator
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)