python中使用time.pref_counter()精確計時


time.pref_counter()返回一個CPU級別的精確時間值,以秒為單位。
它通常用於測量某段程序的運行時間,因此取兩次調用pref_counter()的差值才有意義。

import time

time_start = time.perf_counter()
for i in range(100000):
    a = 0
time_end = time.perf_counter()

time_consumed = time_end - time_start
print("耗費的時間: {} s".format(time_consumed))

耗費的時間: 0.0059451 s


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM