C library function - difftime()
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
- time1 -- This is a time_t object representing the end time.
- time2 -- This is a time_t object representing the start time.
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 -