Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - difftime()

C library function - difftime()

C Standard Library - <time.h> C Standard Library -

Description

C Library Function double difftime(time_t time1, time_t time2) Return time1 and time2 the difference in seconds between (time1 - time2). These two times are specified in calendar time, representing the elapsed time since the Epoch (UTC: 1970-01-01 00:00:00).

Declaration

The following is the declaration of the difftime() function.



double difftime(time_t time1, time_t time2)

Parameters

Return Value

The function returns the difference in seconds between the two times as a double value (time1 - time2).

Example

The following example demonstrates the use 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

(

"

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 exiting...

\

n

"

)

;

return

(

0

)

;

}

Let's compile and run the above program, which will produce the following result:



程序启动...

休眠 5 秒...

执行时间 = 5.000000

程序退出...

C Standard Library - <time.h> C Standard Library -