1. 程式人生 > >python使用裝飾器檢測方法執行時間

python使用裝飾器檢測方法執行時間

"""測試函式執行時間"""
import time

def cal_time(func):
    def call_func():
        print("開始執行")
        start_time = time.time()
        # print(start_time)
        func()
        end_time = time.time()
        # print(end_time)
        print(end_time-start_time,"秒")
    return call_func

@cal_time
def test
():
time.sleep(1) print('test') test()