【发布时间】:2020-09-06 00:58:50
【问题描述】:
我知道 time.perf_counter() 会测量经过的总时间,即使进程当前未运行也是如此。 time.process_time() 但是只测量进程实际运行的时间。
如果我只是衡量一个函数的性能,这两者中的哪一个更受欢迎?
由于我实际上对我的 CPU 用于处理其他进程的时间并不感兴趣,我自然认为 time.process_time() 会是更好的选择(并且在不同的运行中更稳定?),但名称 time.perf_counter( ) 似乎另有说明。
代码示例
import time
from tqdm import trange
start_time_proc = time.process_time()
start_time_perf = time.perf_counter()
tmp = False
for _ in trange(10_000_000):
tmp = not tmp
elapsed_time_proc = time.process_time() - start_time_proc
elapsed_time_perf = time.perf_counter() - start_time_perf
print("process_time:", elapsed_time_proc)
print("perf_counter:", elapsed_time_perf)
【问题讨论】:
标签: python