linux下多线程C++运行时间统计

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;
    }
    

timeval

  • timeval在sys/time.h头文件中,是一个结构体,其中包含经过的秒和微秒,可以通过运算求出比较精确的时间,具体如下面的代码

    #include <iostream>
    #include <pthread.h>
    #include <unistd.h>
    
    #include <ctime>
    #include <sys/time.h>
    #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()
    {
        timeval t_start, t_end;
        cout << "pthread create test" << endl;
    
        int ret = 0;
        pthread_t th_id_1, th_id_2;
    
        gettimeofday( &t_start, 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 );
        }
    
        gettimeofday( &t_end, NULL);
        double delta_t = (t_end.tv_sec-t_start.tv_sec) + 
                        (t_end.tv_usec-t_start.tv_usec)/1000000.0;
        cout << "all time : " << delta_t  << "s" << endl;
    
        return 0;
    }
    

参考链接

猜你喜欢

转载自blog.csdn.net/u012526003/article/details/80862801