C Library Function - difftime()
Description
The C library function double difftime(timet time1, timet time2) returns the difference in seconds between time1 and time2 (time1 - time2). Both times are specified in calendar time, representing the time elapsed since the Epoch (Coordinated Universal Time UTC: 1970-01-01 00:00:00).
Declaration
Below is the declaration for the difftime() function.
double difftime(time_t time1, time_t time2)
Parameters
time1 -- This is the time_t object representing the end time.
time2 -- This is the time_t object representing the start time.
Return Value
The function returns the difference in seconds between the two times (time1 - time2) as a double precision floating-point number.
Example
The following example demonstrates the use of the difftime() function.
#include <stdio.h>
#include <time.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
int main ()
{
time_t start_t, end_t;
double diff_t;
printf("Program start...\n");
time(&start_t);
printf("Sleeping for 5 seconds...\n");
sleep(5);
time(&end_t);
diff_t = difftime(end_t, start_t);
printf("Execution time = %f\n", diff_t);
printf("Program exit...\n");
return(0);
}
Let's compile and run the above program, which will produce the following result:
Program start...
Sleeping for 5 seconds...
Execution time = 5.000000
Program exit...