Monday, 18 July 2016

C program to find time spent by a function

            C program to find time spent by a function         

We write a lot of programs and functions and became happy after finishing it but 
we must care for their complexity in terms of space as well as time.The program or algorithm which takes less time and less memory is the best .So ,if we want to find the time spent by a function then must use the piece of code given below..........
//C program to find time spent by a function
#include<conio.h>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<dos.h>
int main()
{
    int i=0,time_spent;
    clock_t begin;
    begin=clock();
    for(;i<10;i++)
    {   time_spent=(int)(clock()-begin)/CLOCKS_PER_SEC;
        printf("shashi  ");
        delay(1000);//delays the execution for 1 sec
    }
    printf("time spent by the function =%d second",time_spent);
    getch();
    return 0;
}
output:-