perf_counter() 函数的 epoch (纪元)是未定义的。一般使用该函数都是为了比较和计算,不是为了用作绝对时间,该点需要注意下。

该函数用于测量较短持续时间的具有最高有效精度的时钟,包括睡眠状态消耗的时间,使用两次调用才会有效。

测试代码如下:

t0 = time.perf_counter()
# 运行一段代码
for i in range(100000):
    pass
print("程序运行时间", time.perf_counter() - t0)

与其类似的函数有 perf_counter_ns()process_time()process_time_ns(),具体可以查询手册进行学习,先掌握 perf_counter() 函数即可。