linux下多線程C++運行時間統計


CMakeLists.txt

  • 下面所有的代碼的CMakeLists.txt如下

  • cmake_minimum_required(VERSION 2.8)
    
    list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
    
    # else can not fin nullptr and so on.
    set (CMAKE_CXX_STANDARD 11)
    
    #project name
    project(thread_test)
    
    #debug
    set(CMAKE_BUILD_TYPE debug )
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -march=native -fopenmp -g2 -ggdb -lpthread")
    
    # thread_create
    set( EXE_NAME thread_create_test )
    set(APP_SRC thread_create.cpp)
    add_executable(${EXE_NAME} ${APP_SRC} )
    target_link_libraries(${EXE_NAME})

    clock

    • clock函數在ctime中,用於統計cpu時鍾時間,在程序是單線程時,相對准確,但是如果開啟多線程,則會出現很大誤差。如下面的程序

      #include <iostream>
      #include <pthread.h>
      #include <unistd.h>
      
      #include <ctime>
      #include <unistd.h>
      #include <vector>
      
      using namespace std;
      
      void* thread_1(void*)
      {
          // clock_t t;
          // t = clock();
          for (int i = 0; i < 20000; ++i)
          {
              for (int j = 0; j < 20000; ++j)
              {
                  int m = 2*3*4*5*6;
              }
          }
          // t = clock() - t;
          // cout << (unsigned int)pthread_self() << ", time : " << ((double)t)/CLOCKS_PER_SEC << "s" << endl;
      }
      
      
      // the two thread will run arandom order(simutaneously).
      int main()
      {
          cout << "pthread create test" << endl;
      
          int ret = 0;
          pthread_t th_id_1, th_id_2;
      
          clock_t t;
          t = clock();
          std::vector<pthread_t> vec_thread(10);
          for (int i = 0; i < vec_thread.size(); ++i)
          {
              ret = pthread_create( &vec_thread[i], NULL, thread_1, NULL );
              if( ret )
              {
                  cout << "create thread 1 failed..." << endl;
                  return -1;
              }
          }
      
          for (int i = 0; i < vec_thread.size(); ++i)
          {
              pthread_join( vec_thread[i], NULL );
          }
      
          t = clock() - t;
          cout << "all time : " << ((double)t)/CLOCKS_PER_SEC << "s" << endl;
      
          return 0;
      }

      運行需要1s左右,但是會記錄10s左右的時間,因為每個線程中,cpu的clock都在計數,因此這個時間是不准確的。

    • time

      • time也在ctime中,它是記錄系統時間,以s為最小單位,如下面的程序

      • #include <iostream>
        #include <pthread.h>
        #include <unistd.h>
        
        #include <ctime>
        #include <unistd.h>
        #include <vector>
        
        using namespace std;
        
        void* thread_1(void*)
        {
            // clock_t t;
            // t = clock();
            for (int i = 0; i < 20000; ++i)
            {
                for (int j = 0; j < 20000; ++j)
                {
                    int m = 2*3*4*5*6;
                }
            }
            // t = clock() - t;
            // cout << (unsigned int)pthread_self() << ", time : " << ((double)t)/CLOCKS_PER_SEC << "s" << endl;
        }
        
        
        // the two thread will run arandom order(simutaneously).
        int main()
        {
            cout << "pthread create test" << endl;
        
            int ret = 0;
            pthread_t th_id_1, th_id_2;
        
            time_t t;
            t = time(NULL);
            std::vector<pthread_t> vec_thread(10);
            for (int i = 0; i < vec_thread.size(); ++i)
            {
                ret = pthread_create( &vec_thread[i], NULL, thread_1, NULL );
                if( ret )
                {
                    cout << "create thread 1 failed..." << endl;
                    return -1;
                }
            }
        
            for (int i = 0; i < vec_thread.size(); ++i)
            {
                pthread_join( vec_thread[i], NULL );
            }
        
            double cost_t = time(NULL) - t;
            cout << "all time : " << cost_t  << "s" << endl;
        
            return 0;
        }

         


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM