Discuss / Python / 交作业

交作业

Topic source
import time,functools
def metric(fn):
    @functools.wraps(fn)
    def wrapper(*args,**kw):
        start=time.time()
        end=time.time()
        print('%s executed in %s ms'% (fn.__name__,end-start))
        return fn(*args,**kw)
    return wrapper
# 测试
@metric
def fast(x, y):
    time.sleep(1.2)
    return x + y

@metric
def slow(x, y, z):
    time.sleep(3.6)
    return x * y * z

f = fast(11, 22)
s = slow(11, 22, 33)
if f != 33:
    print('测试失败!')
elif s != 7986:
    print('测试失败!')
else:
    print('测试成功!')

  • 1

Reply