def time_count(func):
'''
装饰器
:param func:
:return:
'''
def wrap(*args, **kwargs):
time_flag = time.time()
temp_result = func(*args, **kwargs)
print(temp_result)
print("time cost:", time.time() - time_flag)
return temp_result
return wrap
@time_count
def loop_time(x, y):
temp_result = 0
for i in range(x, y):
time.sleep(random.choice((0.1, 0.2, 0.3)))
temp_result = x + y
return temp_result
loop_time(1, 2)
# 第17、18行代码等同于time_count(loop_time)
网友评论