if unit=='ms' else (t2 - t1)
亲问下,这句话什么意思,是不是说unit是ms的话就*1000不是就不用乘??
亲问下,这句话什么意思,是不是说unit是ms的话就*1000不是就不用乘??
2018-08-17
python3.x环境中,我有个疑问,我把if那段改成如下代码时系统判定为语法错误,有些不明白,希望帮忙解答下
import time
from functools import reduce
def performance(unit):
def perf_decorator(f):
def wrapper(*args, **kw):
t1 = time.time()
r = f(*args, **kw)
t2 = time.time()
if unit=='ms':
t=(t2-t1)*1000
else:
t=(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)
举报