Tag Archives: Decorator

Python 裝飾器 (Decorator) – Python Dev Feed

Python 裝飾器 (Decorator) 假設程式中有一個計算特定事項的函式如下: def works(): total = 0 for i in range(10000): total += i print(“total:”, total) works() Enter fullscreen mode Exit fullscreen mode 在測試的過程中, 我們高度懷疑這個函式效能不彰, 因此希望能幫這個函式計時, 一般的作法可能是這樣: import time def timing(func): print(“Start…”) t1 = time.perf_counter() func() t2 = time.perf_counter() print(“Elapsed time(secs):”, t2 – t1) def works(): total = 0 for i in range(10000):…

Read More