如何度量一个线程的执行时间?
How can I measure the execution time of one thread?
我正在并行运行几个线程。我想测量执行一个线程所需的时间和执行整个程序所需的时间。我用的是vc++,在Windows 7上。
我试着在调试时测量它,但后来我看到了这个问题: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
这是真的吗?如果是的话,我该怎么测量时间呢
谢谢
该语句确实是正确的,只有遇到断点的线程才会被暂停。
然而,要测量执行时间,您根本不必使用调试。关于度量执行时间的更多信息可以在下面的问题中找到:
测量C语言的执行时间(Windows)
你想做的是测量线程函数内部的时间(通过减去函数开始和结束的时间)。您可以对程序做同样的事情,您可以使用thread.join
来确保所有线程的执行在最后一次测量时间之前结束。
使用一个简单的计时器类来创建秒表功能,然后捕获每个线程中的时间。此外,创建系统线程比使用std::async
慢,后者既可以返回值又可以传播异常,使用线程会导致程序终止,除非在线程内捕获。
#include <thread>
#include <iostream>
#include <atomic>
#include <chrono>
#include <future>
// stopwatch. Returns time in seconds
class timer {
public:
std::chrono::time_point<std::chrono::high_resolution_clock> lastTime;
timer() : lastTime(std::chrono::high_resolution_clock::now()) {}
inline double elapsed() {
std::chrono::time_point<std::chrono::high_resolution_clock> thisTime=std::chrono::high_resolution_clock::now();
double deltaTime = std::chrono::duration<double>(thisTime-lastTime).count();
lastTime = thisTime;
return deltaTime;
}
};
// for exposition clarity, generally avoid global varaibles.
const int count = 1000000;
double timerResult1;
double timerResult2;
void f1() {
volatile int i = 0; // volatile eliminates optimization removal
timer stopwatch;
while (i++ < count);
timerResult1=stopwatch.elapsed();
}
void f2() {
volatile int i = 0; // volatile eliminates optimization removal
timer stopwatch;
while (i++ < count);
timerResult2=stopwatch.elapsed();
}
int main()
{
std::cout.precision(6); std::cout << std::fixed;
f1(); std::cout << "f1 execution time " << timerResult1 << std::endl;
timer stopwatch;
{
std::thread thread1(f1);
std::thread thread2(f2);
thread1.join();
thread2.join();
}
double elapsed = stopwatch.elapsed();
std::cout << "f1 with f2 execution time " << elapsed << std::endl;
std::cout << "thread f1 execution time " << timerResult1 << std::endl;
std::cout << "thread f1 execution time " << timerResult2 << std::endl;
{
stopwatch.elapsed(); // reset stopwatch
auto future1 = std::async(std::launch::async, f1); // spins a thread and descturctor automatically joins
auto future2 = std::async(std::launch::async, f2);
}
elapsed = stopwatch.elapsed();
std::cout << "async f1 with f2 execution time " << elapsed << std::endl;
std::cout << "async thread f1 execution time " << timerResult1 << std::endl;
std::cout << "async thread f1 execution time " << timerResult2 << std::endl;
}
在我的机器上创建线程每个线程增加0.3 ms,而异步每个线程只有0.05 ms,因为它是用线程池实现的。
f1 execution time 0.002076
f1 with f2 execution time 0.002791
thread f1 execution time 0.002018
thread f1 execution time 0.002035
async f1 with f2 execution time 0.002131
async thread f1 execution time 0.002028
async thread f1 execution time 0.002018
[EDIT]在语句前有不正确的f调用(cut and past error)
相关文章:
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- C++线程:如何在一个线程仍在运行时阻止另一个线程执行 (Win32)
- 在给定时间段内,线程执行的指令数量是否有最小数量?
- 异步控制线程执行时间
- 如何在C++中停止线程执行
- 为什么Win API线程执行函数,而标准线程则没有
- 线程执行在IDE中和C 中的EXE应用程序中是否有所不同
- 从线程执行的函数中返回结构数组
- C++线程安全时间计数类
- 为什么在从线程执行方法时使用QMetaObject::invokeMethod
- 只有一个线程执行 CUDA 内核
- C++ 多线程:执行顺序
- async_connect()超时,多个线程执行io_service.run()
- 是否可以将线程执行转移到另一个线程?
- 具有随机数总和的多线程执行时间
- 如何 在没有 Boost 的情况下将 C++ 成员函数作为线程执行
- C++11 线程:剩余时间的睡眠
- openMP 在自定义容器中从 2 到 4 个线程执行二进制搜索时速度变慢
- 如何使生成的线程拖延足够长的时间以使生成线程执行某些操作
- C++ 线程执行时间和另一个线程中的执行线程