如何测量一个线程的执行时间? [英] How can I measure the execution time of one thread?

查看:534
本文介绍了如何测量一个线程的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我尝试在调试时对其进行测量,但是随后我看到了这个问题:

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

谢谢

解决方案

该语句确实是正确的,只有遇到断点的线程才会暂停.

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

测量C语言(在Windows上)的执行时间

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

I'm running a couple of threads in parallel. And I want to measure the time it takes to execute one thread and the time it takes to execute the whole program. I'm using VC++, on Windows 7.

I tried to measure it while debugging but then I saw this question: https://stackoverflow.com/questions/38971267/improving-performance-using-parallelism-in-c?noredirect=1#comment65299718_38971267 and in the answer given by Schnien it says:

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 

Is this true ? And if yes how can I otherwise measure the time

Thanks

解决方案

That statement is indeed true, only the thread that hits a breakpoint will be paused.

However to measure execution times you do not have to use debugging at all. More information on measuring execution time can be found on the below question:

Measure execution time in C (on Windows)

What you would want to do is measure the time inside the threads' functions (by subtracting the time at the beginning and at the end of the functions). You can do the same with the program, you can use thread.join to make sure all the threads executions end before measuring the time one last time.

这篇关于如何测量一个线程的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆