Calculating running program time - C Programming Tutorial

In this c programming tutorial we are going to see how we can calculate actual running time for any c program or algorithm implementation. Below is the code calculates running time for sorting 9999 elements using bubble sort. Note that on every machine you may get different timings.

#include<stdio.h>#include<time.h>#include<stdlib.h>#define size 9999void main(){      int a[size],i,j,temp,num;      clock_t start,end;      for(i=0;i<size;i++)	{		num=rand()%size+1;		a[i]=num;	}      start=clock();      for(i=0;i<size;i++)      {      for(j=0;j<size-i;j++)      {                           if(a[j]>a[j+1])                           {                                          temp=a[j];                                          a[j]=a[j+1];                                          a[j+1]=temp;                           }      }      }      end=clock();      printf("\nTime taken=%lfs\n",(double)(end-start)/CLOCKS_PER_SEC);}

 Output:

how to calculate time of program code in c

Comments

Popular posts from this blog

MATLAB code for Circular Convolution using Matrix method

Positive number pipe in angular 2+