首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python和time模块中的多线程

python和time模块中的多线程
EN

Stack Overflow用户
提问于 2022-09-13 17:25:27
回答 1查看 51关注 0票数 1

我正在尝试学习python中的multi_threading,但是当我试图打印time.perf_counter()以查看主线程运行程序所需的时间(负责程序)时,输出是一个巨大的数字( (614691.9609577) ),但它甚至不应该是2秒。你能解释一下这个问题的原因吗?谢谢

代码语言:javascript
复制
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()) 
EN

回答 1

Stack Overflow用户

发布于 2022-09-13 17:34:23

来自这里

time.perf_counter():返回性能计数器的值(以小数秒为单位),即具有最高可用分辨率的时钟来测量短时间。它确实包括睡眠期间经过的时间,并且是全系统的.返回值的引用点未定义,因此只对两个调用的结果之间的差异有效。 使用perf_counter_ns()可以避免浮点数类型造成的精度损失。 新版本3.3。 在3.10版本中更改:在Windows上,该函数现在是系统范围的.

在你的情况下,如果你想测量时间,你需要减去两个时间间隔。例如:

代码语言:javascript
复制
t1_start = time.perf_counter()
# your code...
t2_stop = time.perf_counter()
print("Elapsed time: ", t2_stop - t1_start)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73707042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档