Reputation: 137
I want to ask for help to measure the execution time of matrix multiplication below. I'm running the code in Windows. I've try to using time.h, but it can't measured. Where I have to put the timer?
#include<stdio.h>
#include<math.h>
#include<time.h>
void main()
{
int m1[10][10],i,j,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2;
/*double dif;
time_t start, end;*/
printf("Enter number of rows and columns of first matrix MAX 10\n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of second matrix MAX 10\n");
scanf("%d%d",&r2,&c2);
if(r2==c1)
{
printf("Enter rows and columns of First matrix \n");
printf("Row wise\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
}
printf("You have entered the first matrix as follows:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",m1[i][j]);
printf("\n");
}
printf("Enter rows and columns of Second matrix \n");
printf("Again row wise\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
printf("You have entered the second matrix as follows:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[i][j]);
printf("\n");
}
/*time (&start);*/
printf("Now we multiply both the above matrix \n");
printf("The result of the multiplication is as follows:\n");
/*a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33*/
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
mult[i][j]=0;
for(k=0;k<r1;k++)
{
mult[i][j]+=m1[i][k]*m2[k][j];
/*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
}
printf("%d\t",mult[i][j]);
}
printf("\n");
/*time (&end);
dif (difftime (end, start);
printf("Time of execution is : %f\n",dif)*/
}
getch();
}
else
{
printf("Matrix multiplication cannot be done");
}
}
I want the measurement as accurate as possible.
Upvotes: 1
Views: 2822
Reputation: 3678
the time function's precision, so if the code executes less than 1 second, you can't get correct output. On windows, I prefer to use GetTickCount
sample
#include "Windows.h"
int main (void)
{
DWORD start,end;
start = GetTickCount();
//do something like Sleep(1000)
end = GetTickCount();
printf("elapse %d milliseconds\n", end - start);
return 0;
}
Upvotes: 1
Reputation: 754110
I think you'd be best off with your 'end time' code outside the loops you've currently got, immediately before the call to getch()
. This would give you the maximum chance of counting more than 1 second. To get a decent measure, you probably need to repeat the whole multiplication a number of times (so that the total elapsed time is measured in 10s of seconds). You should avoid printing in the loop, too; the printing time will likely dominate the calculation time.
The rest of your trouble is that the time()
system call provides 1 second resolution on the timing. You really need a timing routine with sub-second resolution such as gettimeofday()
(microsecond) or clock_gettime()
(nanosecond). Note that resolution and accuracy are different. (You could use clock()
instead, which is Standard C but normally provides much less resolution. Historically, there was also ftime()
and times()
that could be used. These gave millisecond resolution.) There are other system calls available on Windows. You will still want a significant repeat count to make the timing useful (1000 times, or 10,000 times, or 1,000,000 times) because it does not take long to do a 10x10 matrix multiplication.
Upvotes: 2
Reputation: 140
How about using clock()
?
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <time.h>
int main() {
clock_t start, stop;
double t = 0.0;
/* Start timer */
start = clock();
assert(start != -1);
/* Perform calculations */
/* Stop timer */
stop = clock();
t = (double) (stop-start)/CLOCKS_PER_SEC;
printf("Run time: %f\n", t);
return(0);
} /* main */
Upvotes: 0