如何度量一个线程的执行时间?

How can I measure the execution time of one thread?

本文关键字:线程 执行时间 一个 何度量 度量      更新时间:2023-10-16

我正在并行运行几个线程。我想测量执行一个线程所需的时间和执行整个程序所需的时间。我用的是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)