"""
写一个装饰器
用来检测任意一段代码的执行时间 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_time 10 def test_one(*x): 11 sum1 = 0 12 for i in range(15000000): 13 sum1 += 1 14 @test_time 15 def test_two(*x): 16 sum1 = 0 17 for i in range(100000): 18 sum1 += 1 19 test_one() 20 test_two()
结果
1.0608017444610596 0.015599966049194336