簡體   English   中英

如何測量一個線程的執行時間?

[英]How can I measure the execution time of one thread?

我正在並行運行幾個線程。 而且我想測量執行一個線程所需的時間和執行整個程序所需的時間。 我在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 

這是真的 ? 如果是的話,我該如何測量時間

謝謝

該語句確實是正確的,只有到達斷點的線程才會暫停。

但是,要測量執行時間,您根本不必使用調試。 有關測量執行時間的更多信息,請參見以下問題:

在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毫秒,而異步僅為每個線程大約.05毫秒,因為它是通過線程池實現的。

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調用(剪切和過去的錯誤)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM