2016-08-16 6 views
1

私はいくつかのスレッドを同時に実行しています。そして、私はスレッドを実行するのにかかる時間と、プログラム全体を実行するのに要する時間を測定したいと思います。 https://stackoverflow.com/questions/38971267/improving-performance-using-parallelism-in-c?noredirect=1#comment65299718_38971267とSchnienによって与えられた答えに、それは言う:1つのスレッドの実行時間をどのように測定できますか?

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 

はこの本当です私は、デバッグ中に、私はそれを測定しようとしたが、その後、私はこの質問を見たのWindows 7上で、

をVC++を使用していますか?どのように私はそれ以外の時間に文は確かに真実であることを

おかげ

+0

組み込みのプロファイラや同時実行ビジュアライザ使用:https://msdn.microsoft.com/en-us/ライブラリ/ dd537632.aspx – Mars

答えて

2

を測定することができそうなら、ブレークポイントにヒットする唯一のスレッドが一時停止されます。

実行時間を測定するには、デバッグをまったく使用する必要はありません。最初に時間を減算して(スレッドの関数内の時間を測定し、最後にされ、あなたがしたいと思う何

Measure execution time in C (on Windows)

:実行時間を計測の詳細については、下記の質問に見つけることができます関数の)。あなたはプログラムで同じことをすることができます。thread.joinを使って、前回の時間を測定する前にすべてのスレッドの実行が終了するようにすることができます。

1

単純なタイマークラスを使用してストップウォッチ機能を作成し、各スレッド内の時間を取得します。また、システムスレッドの作成はstd::asyncを使用するよりも遅く、後者は値を返して例外を伝播することができ、スレッドを使用するとスレッド内で捕捉されない限りプログラムの終了を引き起こします。それはスレッドプールで実装されているスレッドあたりたったの約0.05ミリ秒であるのに対し、非同期私のマシンがスレッドを作成するには

#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ミリ秒を追加します。

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コール(カットと過去のエラー)

関連する問題