C library functions -difftime()

C Standard Library - <time.h>

Description

C Library Functionsdouble difftime(time_t time1, time_t time2)returntime1andtime2The number of seconds between(time1 - time2)These two times are specified in calendar time, representing the elapsed time since the Epoch (Coordinated Universal Time UTC: 1970-01-01 00:00:00).

Declaration

Below is the declaration of the difftime() function.

double difftime(time_t time1, time_t time2)

Parameter

  • time1-- This is the time_t object representing the end time.
  • time2-- This is the time_t object representing the start time.

Return Value

This function returns the number of seconds between the two times (time1 - time2) as a double-precision floating-point double value.

Example

The following example demonstrates the usage of the difftime() function.

Example

#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 starting...\n"); time(&start_t); printf("Sleep for 5 seconds...\n"); sleep(5); time(&end_t); diff_t = difftime(end_t, start_t); printf("Execution time = %f\n", diff_t); printf("Exiting program...\n"); return(0); }

Let us compile and run the above program, which will produce the following results:

程序启动...
休眠 5 秒...
执行时间 = 5.000000
程序退出...

C Standard Library - <time.h>

other extensions