python ——装饰器
import time def run_time(fun): def wrapper(): start=time.time() fun()#函数在这里执行 end=time.time() cost_time=end-start print("耗时",cost_time) return wrapper @run_time def fun_one(): for i in range(0,10): print(i) time.sleep(0.5) @run_time def fun_two(): for i in range(10,16): print(i) time.sleep(0.6) fun_one() fun_two()
import time def fun_one(): t0=time.time() for i in range(0,10): print(i) t1=time.time() total_time=t1-t0 print(total_time) def fun_two(): t0=time.time() for i in range(11,32): print(i) t1=time.time() total_time=t1-t0 print(total_time) def fun_three(): t0=time.time() for i in range(110,120): print(i) t1=time.time() total_time=t1-t0 print(total_time) fun_one() fun_two() fun_three()