python笔记之time模块

方法一: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为需要推迟的秒数

猜你喜欢

转载自www.cnblogs.com/huwt/p/10341419.html