Measure execution time in C++ OpenMP code

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


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.