gpt4 book ai didi

c++ - 如何测量一个线程的执行时间?

转载 作者:行者123 更新时间:2023-11-30 01:42:47 24 4
gpt4 key购买 nike

我正在并行运行几个线程。我想测量执行一个线程所花费的时间和执行整个程序所花费的时间。我在 Windows 7 上使用 VC++。

我试图在调试时测量它,但后来我看到了这个问题:https://stackoverflow.com/questions/38971267/improving-performance-using-parallelism-in-c?noredirect=1#comment65299718_38971267在 Schnien 给出的答案中它说:

Debugging of multiple threads is somehow "special" - when your Debugger halts at a breakpoint, the other threads will not be stopped - they will go on 

这是真的吗?如果是,我该如何测量时间

谢谢

最佳答案

这句话确实没错,只有命中断点的线程才会被暂停。

然而,要测量执行时间,您根本不必使用调试。有关测量执行时间的更多信息,请参见以下问题:

Measure execution time in C (on Windows)

您想要做的是测量线程函数内的时间(通过减去函数开始和结束的时间)。您可以对程序执行相同的操作,您可以使用 thread.join 确保所有线程执行在最后一次测量时间之前结束。

关于c++ - 如何测量一个线程的执行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38977879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com