Thursday, 10 March 2016

How to find the execution time of a C program

                         In this post, we will see how to find the required execution time of a program written in C language.
                         Here we have to include header file time.h. clock_t is a data type which represents the number of clock ticks. Function clock() returns the CPU time in terms of clock ticks. CLOCKS_PER_SEC is a macro which has the value of number of clock ticks per second.
                         To get the execution time, we have to find difference between beginning time and end time.
                          Go through the following program to understand thoroughly. 


Program: (time.c)


#include<stdio.h>
#include<time.h>
void main()
{
clock_t starttime;
clock_t endtime;
int i,sum;
double timeinterval;
starttime=clock();
for(i=1;i<=100;i++)
  {
    sum=sum+i;  
  }
endtime=clock();
timeinterval=(double)(endtime-starttime)/CLOCKS_PER_SEC;
printf("Time required for execution=%lf Seconds.\n",timeinterval);
}

How To Run:
To Compile:
gcc time.c

To Run:
./a.out

Output:


Next: How to reverse a Linked List in C Language

Previous: Client Server program in Java using Socket (One Server and Multiple Clients)




No comments:

Post a comment