多线程应用程序的挂起时间分析

时间:2018-06-11 11:13:05

标签: c++ multithreading performance wall-time

我有两个应用程序:一个是多线程的,一个是完全顺序的。两个应用程序执行相同的任务。我试图计算多线程应用程序的加速。不知何故,与顺序代码(147.19 us)相比,我获得了多线程代码(41 us)的更长时间。墙壁时间剖析还有其他方法吗?

#include "iostream"
#include "ctime"
#include "thread"
#include "chrono"
#include "iomanip"

#include <sys/time.h>

int deltaTime(struct timeval *tv1, struct timeval *tv2){
    return ((tv2->tv_sec - tv1->tv_sec)*1000000)+ tv2->tv_usec - tv1->tv_usec;
}

void execute_for_wallTime(int wall_time) 
{
    struct timeval  tvStart, tvNow;
    gettimeofday(&tvStart, NULL);

    for (int m = 0; wall_time; ++m){
      gettimeofday(&tvNow, NULL);
      if(deltaTime(&tvStart,&tvNow) >=wall_time) { 

        return;
      }
   } 
}

int sc_main(int argc, char* argv[])
{
   std::clock_t c_start = std::clock();
   auto t_start = std::chrono::high_resolution_clock::now();


   //multi-thread code 

   // std::thread t1(execute_for_wallTime, 10);
   // std::thread t2(execute_for_wallTime, 13);
   // std::thread t3(execute_for_wallTime, 16);
   // t1.join();
   // t2.join();
   // t3.join();

   //Sequential Code

   execute_for_wallTime(10);
   execute_for_wallTime(13);
   execute_for_wallTime(16);

   std::clock_t c_end = std::clock();
   auto t_end = std::chrono::high_resolution_clock::now();
   std::cout << std::fixed << std::setprecision(2) << "CPU time used: "
             << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " ms\n"
             << "Wall clock time passed: "
             << std::chrono::duration<double, std::micro>(t_end- 
                t_start).count()
             << " us\n";   

    return 0;
}

0 个答案:

没有答案