Measure execution time in C++ OpenMP code

前端 未结 5 1053
粉色の甜心
粉色の甜心 2020-11-27 05:46

I am running a .cpp code (i) in sequential style and (ii) using OpenMP statements. I am trying to see the time difference. For calculating time, I use this:

         


        
相关标签:
5条回答
  • 2020-11-27 06:15

    I've seen clock() reporting CPU time, instead of real time.

    You could use

    struct timeval start, end;
    gettimeofday(&start, NULL);
    
    // benchmark code
    
    gettimeofday(&end, NULL);
    
    delta = ((end.tv_sec  - start.tv_sec) * 1000000u + 
             end.tv_usec - start.tv_usec) / 1.e6;
    

    To time things instead

    0 讨论(0)
  • 2020-11-27 06:16

    You could use the built in get time function in omp library itself. Here is the code

    #include <stdio.h>
    #include <omp.h>
    
    int main(){
    
        double itime, ftime, exec_time;
        itime = omp_get_wtime();
    
        // Required code for which execution time has to be found
        
        ftime = omp_get_wtime();
        exec_time = ftime - itime;
        printf("\n\nTime taken is is %f", exec_gap);
    
    }
    
    0 讨论(0)
  • 2020-11-27 06:17

    It seems to me as though the clock () function is calculating each thread's individual time and adding up them up and displaying them.

    This is exactly what clock() does - it measures the CPU time used by the process, which at least on Linux and Mac OS X means the cumulative CPU time of all threads that have ever existed in the process since it was started.

    Real-clock (a.k.a. wall-clock) timing of OpenMP applications should be done using the high resolution OpenMP timer call omp_get_wtime() which returns a double value of the number of seconds since an arbitrary point in the past. It is a portable function, e.g. exists in both Unix and Windows OpenMP run-times, unlike gettimeofday() which is Unix-only.

    0 讨论(0)
  • 2020-11-27 06:17
    #include "ctime"
    
    std::time_t start, end;
    long delta = 0;
    start = std::time(NULL);
    
    // do your code here
    
    end = std::time(NULL);
    delta = end - start;
    
    // output delta
    
    0 讨论(0)
  • Well yes, that's what clock() is supposed to do, tell you how much processor time the program used.

    If you want to find elapsed real time, instead of CPU time, use a function that returns wall clock time, such as gettimeofday().

    0 讨论(0)
提交回复
热议问题