There are many methods to calculate time taken by part of execution or whole program in gcc as mention below.
Out of this time() function gives the accuracy up-to seconds. But generally we require accuracy in micro or nano seconds to check the little difference. So we are going to see the time calculation using clock_gettime() and gettimeofday() function for well known application matrix multiplication.
The programs matrix_mul_gettimeofday.c given below show the time calculated using gettimeofday() function
gcc matrix_mul_gettimeofday.c -o exec
Run:
./exec
The program matrix_mul_clock_gettime.c given below will show how to calculate time using clock_gettime() function
compile:
gcc matrix_mul_clock_gettime.c -o exec -lm
Run:
./exec
Out of this time() function gives the accuracy up-to seconds. But generally we require accuracy in micro or nano seconds to check the little difference. So we are going to see the time calculation using clock_gettime() and gettimeofday() function for well known application matrix multiplication.
The programs matrix_mul_gettimeofday.c given below show the time calculated using gettimeofday() function
#include"stdio.h" #include"sys/time.h" #include"stdlib.h" #define N 4000 #define X 300 #define Y 200 int main() { int sum = 0,i,j,k; int **mat1; //declared mat1[X][N] int **mat2; //declare mat2[N][Y] int **mat_res; //resultant matrix become mat_res[X][Y] struct timeval tim; double t1,t2,result; /*----------------------------------------------------*/ //create array of pointers(Rows) mat1 =(int **) malloc(X*sizeof(int*)); mat2 =(int **)malloc(N*sizeof(int*)); mat_res=(int **)malloc(X*sizeof(int*)); /*----------------------------------------------------*/ /*--------------------------------------------------------------------------------*/ //allocate memory for each Row pointer for(i=0; i < X; i++) { mat1[i] = (int *) malloc(N * sizeof(int)); mat_res[i] = (int *) malloc(Y * sizeof(int)); } for(i = 0; i < N; i++) mat2[i] = (int *) malloc(Y * sizeof(int)); /*--------------------------------------------------------------------------------*/ for(i = 0; i < X; i++) { for(j = 0; j < N; j++) { mat1[i][j] = 1; } } for(i = 0; i < N; i++) { for(j = 0; j < Y; j++) { mat2[i][j] = 2; } } //------------------calculate Start time------------------------ gettimeofday(&tim, NULL); t1 = tim.tv_sec + (tim.tv_usec/1000000.0); //-------------------------------------------------------------- for(i = 0; i < X; i++) { for(j = 0; j < Y;j++) { sum=0; for(k = 0; k < N; k++) { sum = sum + mat1[i][k] * mat2[k][j]; } mat_res[i][j] = sum; } } //---------------calculate End time----------------------------- gettimeofday(&tim, NULL); t2 = tim.tv_sec + (tim.tv_usec/1000000.0); //-------------------------------------------------------------- // result = End_time - Start_time result = t2 - t1; for(i = 0; i < X; i++) { for(j = 0; j < Y; j++) { printf("%d\t",mat_res[i][j]); } printf("\n"); } printf("time taken =%f\n",result); return 0; }Compile:
gcc matrix_mul_gettimeofday.c -o exec
Run:
./exec
The program matrix_mul_clock_gettime.c given below will show how to calculate time using clock_gettime() function
#include"stdio.h" #include"time.h" #include"math.h" #include"stdlib.h" #define N 4000 #define X 300 #define Y 200 int main() { int sum = 0,i,j,k; int **mat1; //declared mat1[X][N] int **mat2; //declare mat2[N][Y] int **mat_res; //resultant matrix become mat_res[X][Y] struct timespec start,stop; double t1=0,t2=0,result=0; /*----------------------------------------------------*/ //create array of pointers(Rows) mat1 =(int **) malloc(X*sizeof(int*)); mat2 =(int **)malloc(N*sizeof(int*)); mat_res=(int **)malloc(X*sizeof(int*)); /*----------------------------------------------------*/ /*--------------------------------------------------------------------------------*/ //allocate memory for each Row pointer for(i=0; i < X; i++) { mat1[i]=(int *)malloc(N*sizeof(int)); mat_res[i]=(int *)malloc(Y*sizeof(int)); } for(i = 0; i < N; i++) mat2[i]=(int *)malloc(Y*sizeof(int)); /*--------------------------------------------------------------------------------*/ for(i = 0; i < X; i++) { for(j = 0; j < N; j++) { mat1[i][j] = 1; } } for(i = 0; i < N; i++) { for(j = 0; j < Y; j++) { mat2[i][j] = 2; } } //------------------calculate Starting time---------------------- clock_gettime(CLOCK_REALTIME,&start); t1 = start.tv_sec + (start.tv_nsec/pow(10,9)); //-------------------------------------------------------------- for(i = 0; i < X; i++) { for(j = 0; j < Y;j++) { sum=0; for(k = 0; k < N; k++) { sum = sum + mat1[i][k] * mat2[k][j]; } mat_res[i][j] = sum; } } //---------------calculate End time------------------------- clock_gettime(CLOCK_REALTIME,&stop); t2 = stop.tv_sec + (stop.tv_nsec/pow(10,9)); //-------------------------------------------------------------- // result = End_time - Start_time result = t2 - t1; for(i = 0; i < X; i++) { for(j = 0; j < Y; j++) { printf("%d\t",mat_res[i][j]); } printf("\n"); } printf("\ntime taken =\t %lf\n",result); return 0; }In this program CLOCK_REALTIME means choose best wall clock timing of machines which is defined in time.h header file.
compile:
gcc matrix_mul_clock_gettime.c -o exec -lm
Run:
./exec
0 comments:
Post a Comment