import time def cost_time(func): def inner(): t1 = time.perf_counter() func() t2 = time.perf_counter() print(t2 - t1) return inner @cost_time def step_two(): for i in range(10000): print(i) def step_one(): t1 = time.perf_counter() for i in range(10000): print(i) t2 = time.perf_counter() print(t2 - t1) if __name__ == '__main__': step_one() step_two()
装饰器常用用法:
引入日志
函数执行时间统计
执行函数前预处理
执行函数前的清理功能
权限校验
等等