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