计算某个程序运行时间间隔是个非常常见的小功能。用Python中的time模块的perf_counter()函数即可
代码语言:javascript复制import time
t1=time.perf_counter()
for i in range(10):
time.sleep(1)
print(f'run{i}')
t2=time.perf_counter()
print(f'运行时长{t2-t1}s')
运行结果如下:
run0
run1
run2
run3
run4
run5
run6
run7
run8
run9
运行时长10.0913788s