1. 程式人生 > 其它 >python 報錯 AttributeError: module ‘time‘ has no attribute ‘clock 解決方法

python 報錯 AttributeError: module ‘time‘ has no attribute ‘clock 解決方法

原始碼如下:

#引入所需要的時間庫
import datetime
import time
 
#程式計時器,啟動計時器
start = time.clock()
 
#中間是放置需要測試執行時間的程式程式碼
 
#計算啟動時間和結束時間的時間差
end = time.clock()
print('執行時間 : %s 秒'%(end-start))

報錯AttributeError module 'time' has no attribute 'clock'

解決方法:python3.8不支援 clock 了,用 time.perf_counter() 替換就可以了