我正在尝试学习python中的multi_threading,但是当我试图打印time.perf_counter()以查看主线程运行程序所需的时间(负责程序)时,输出是一个巨大的数字( (614691.9609577) ),但它甚至不应该是2秒。你能解释一下这个问题的原因吗?谢谢
import threading
import time
def have_breakfast():
time.sleep(3)
print("You had breakfast")
def make_bed():
time.sleep(4)
print("You made your bed")
def study():
time.sleep(5)
print("You finish studying")
x = threading.Thread(target = have_breakfast, args = ())
x.start()
y = threading.Thread(target = make_bed, args = ())
y.start()
z = threading.Thread(target = study, args = ())
z.start()
x.join()
print(threading.active_count())
print(threading.enumerate())
print(time.perf_counter()) 发布于 2022-09-13 17:34:23
来自这里
time.perf_counter():返回性能计数器的值(以小数秒为单位),即具有最高可用分辨率的时钟来测量短时间。它确实包括睡眠期间经过的时间,并且是全系统的.返回值的引用点未定义,因此只对两个调用的结果之间的差异有效。 使用perf_counter_ns()可以避免浮点数类型造成的精度损失。 新版本3.3。 在3.10版本中更改:在Windows上,该函数现在是系统范围的.
在你的情况下,如果你想测量时间,你需要减去两个时间间隔。例如:
t1_start = time.perf_counter()
# your code...
t2_stop = time.perf_counter()
print("Elapsed time: ", t2_stop - t1_start)https://stackoverflow.com/questions/73707042
复制相似问题