""" 写一个装饰器 用来检测任意一段代码的执行时间 time.time 利用不定长参数实现 *x """
1 import time 2 def test_time(defx): 3 def test_time1(*x): 4 start1 = time.time() 5 defx(*x) 6 exit1 = time.time() 7 print(exit1 - start1) 8 return test_time1 9 @test_time10 def test_one(*x):11 sum1 = 012 for i in range(15000000):13 sum1 += 114 @test_time15 def test_two(*x):16 sum1 = 017 for i in range(100000):18 sum1 += 119 test_one()20 test_two()
结果
1.06080174446105960.015599966049194336