Python學習-一個簡單的計時器
在實際開發中,往往想要計算一段代碼執行多長時間,以下我將該功能寫入到一個函數裏面,僅僅要在每一個函數前面調用該函數就可以,見以下代碼:
#--------------------------------
#一個記時器,[email protected]_timer就可以
import time
from functools import wraps
def fun_timer(function):
@wraps(function)
def function_timer(*args, **kwargs):
t0 = time.time()
result = function(*args, **kwargs)
t1 = time.time()
os.system(" echo Total time running %s: %s seconds" % (function.func_name, str(t1-t0)) + " >> timecount.log")
return result
return function_timer
#-----------------------------------
Python學習-一個簡單的計時器