方法一:time.perf_counter()
說明:返回計時器的精准時間(系統的運行時間),包含整個系統的睡眠時間。由於返回值的基准點是未定義的,所以,只有連續調用的結果之間的差才是有效的。
代碼示例:
import time start_perf = time.perf_counter() time.sleep(2) end_perf = time.perf_counter() print(end_perf - start_perf)
運行結果:
2.0041295609999996 [Finished in 2.2s]
方法二:time.process_time()
說明:返回當前進程執行 CPU 的時間總和,不包含睡眠時間。由於返回值的基准點是未定義的,所以,只有連續調用的結果之間的差才是有效的。
代碼示例:
import time start_process = time.process_time() time.sleep(2) end_process = time.process_time() print(end_process - start_process)
運行結果:
0.0 [Finished in 2.1s]
方法三:time.sleep(secs)
說明:
推遲線程的運行,沒有返回值,參數secs為需要推遲的秒數