Reputation: 117
I am trying to measure a multi-thread program's execution time. I've use this piece of code in main program for calculating time:
clock_t startTime = clock();
//do stuff
clock_t stopTime = clock();
float secsElapsed = (float)(stopTime - startTime)/CLOCKS_PER_SEC;
Now the problem i have is: for example I run my program with 4 thread(each thread running on one core), the execution time is 21.39 . I check my system monitor in run time, where the execution time is about 5.3.
It seems that the actual execution time is multiplied by the number of THREADS.
What is the problem??
Upvotes: 8
Views: 7155
Reputation: 4685
It is because you are monitoring the CPU time which is the accumulated time spent by CPU executing your code and not the Wall time which is the real-world time elapsed between your startTime
and stopTime
.
Indeed clock_t :
Returns the processor time consumed by the program.
If you do the maths : 5.3 * 4 = 21.2
which is what you obtain meaning that you have good multithreaded code with a speedup of 4
.
So to measure the wall time, you should rather use std::chrono::high_resolution_clock for instance and you should get back 5.3
. You can also use the classic gettimeofday()
.
If you use OpenMP for multithreading you also have omp_get_wtime()
double startTime = omp_get_wtime();
// do stuff
double stopTime = omp_get_wtime();
double secsElapsed = stopTime - startTime; // that's all !
Upvotes: 10