1. 程式人生 > >day 13 函式裝飾器

day 13 函式裝飾器

"""
寫一個裝飾器
用來檢測任意一段程式碼的執行時間 time.time
利用不定長引數實現 *x
"""
import time
def test_time(defx):
def test_time1(*x):
start1 = time.time()
defx(*x)
exit1 = time.time()
print(exit1 - start1)
return test_time1

@test_time
def test_one(*x):
sum1 = 0
for i in range(15000000):
sum1 += 1

print(x + x + x)
print(sum1)

@test_time
def test_two(*x):
sum1 = 0
for i in range(100000):
sum1 += 1
print(x + x)
print(sum1)

test_one(1, 2, 3)
test_two(3, 4)