2016-08-16 35 views
1

我正在并行运行一些线程。我想测量执行一个线程所需的时间以及执行整个程序所需的时间。我使用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 

这是真的吗?如果是我怎么能以其他方式测量时间

感谢

+0

使用内置探查或并发可视电话˚F库/ dd537632.aspx – Mars

回答

2

该声明的确是真实的,只有遇到断点线程将被暂停。

但是,为了衡量执行时间,您根本不必使用调试。在测量执行时间更多信息,可以在下面的问题中找到:

Measure execution time in C (on Windows)

,你会想要做的是测量线程的功能,里面的时间(通过在开始减去时间和结束时的功能)。您可以对该程序执行相同的操作,您可以使用thread.join来确保在最后一次测量时间之前所有线程的执行都结束。

1

使用简单的计时器类创建秒表功能,然后捕获每个线程中的时间。另外,创建系统线程比使用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毫秒,而异步是每个线程只有大约0.05毫秒,因为它是一个线程池来实现。 https://msdn.microsoft.com/en-us/:

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 

[编辑]本来不正确的语句的前面(削减和过去的错误)