by Dinesh Thakur Category: Function and Pointer

The function difftime ()returns the difference between two calendar times in seconds, i.e., number of seconds elapsed between two calendar times time2 and time]. The calendar time represents the time elapsed since 00:00:00 hours 01 January 1970, GMT. The function returns the difference in seconds as a double number. The function prototype is written as follows:

double difftime (time_t Time2, time_t Time1);

Application of this function is illustrated in Program in which an initial time Timel is determined, and a second time Time2 is determined after a long calculation in the program. The difftime () computes difference of the two times.

Illustrates difftime () function

#include<stdio.h>

#include<time.h>
void main()
{
  int sum = 1 ;
  int i, j, n = 10000000;
  time_t Time1, Time2;
  Time1 = time(&Time1);
  clrscr();
  printf("Time1 = %u\n", Time1);
  for ( i=0 ; i<n; i++)
    for (j=0; j<60; j++)
      sum+= j;
      Time2 = time(&Time2);
      //A large loop
      //record the time
      printf("Time2 = %u\n", Time2);
      printf("Difference of two times= %lf\n", difftime(Time2,Time1));
}

Function difftime () in C



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles