1 def timer(function): 2 """ 3 装饰器函数timer 4 :param function:想要计时的函数 5 :return: 6 """ 7 8 def wrapper(*args, **kwargs): 9 time_start = time.time() 10 res = function(*args, **kwargs) 11 cost_time = time.time() - time_start 12 print("【%s】运行时间:【%s】秒" % (function.__name__, cost_time)) 13 return res 14 15 return wrapper 16 17 18 @timer 19 def main(): 20 time.sleep(1) 21 22 23 if __name__ == '__main__': 24 main()