0

我有两个应用程序:一个是多线程的,一个是完全顺序的。两个应用程序执行相同的任务。我正在尝试计算多线程应用程序的加速。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;
}
4

0 回答 0