I am executing the code to calculate the time taken by a Matrix multiplication Code.
I have created four threads and called the Calculate method like this:
std::thread t1( Calculate );
std::thread t2( Calculate );
std::thread t3( Calculate );
std::thread t4( Calculate );
t1.join();
t2.join();
t3.join();
t4.join();
This is the code where I am doing the matrix multiplication
void calculate()
{
clock_t starttime = clock();
// some Code
clock_t endtime = clock();
cout << "Time Taken:"<<diffclock(endtime, starttime)<<"sec."<<endl;
}
This is the method to calculate time difference:
double diffclock(clock_t clock1,clock_t clock2)
{
double diffticks=clock1-clock2;
double diffms=(diffticks)/CLOCKS_PER_SEC;
return diffms;
}
After the execution the time taken by whole execution is displayed incorrectly. The time taken by the operation is around 22 seconds but the code is giving nearly 32 seconds as time taken. I have checked it from stopwatch and the output by this code is Incorrect.
As per the documentation of clock
In order to measure the time spent in a program, clock() should be called
at the start of the program and its return value subtracted from the value
returned by subsequent calls. The value returned by clock() is defined for
compatibility across systems that have clocks with different resolutions.
To determine the time in seconds, the value returned by clock() should be
divided by the value of the macro CLOCKS_PER_SEC. CLOCKS_PER_SEC is defined
to be one million in <time.h>.
However the time returned by this time calculating code contradicts with the time provided by the IDE. I am using code::blocks here.
Am I missing some thing?