Sometimes in practical applications, you calculate the length of time an event lasts, such as calculating typing speed. In the 1th timer section, I've used the clock function to give an example. The Clock () function can be accurate to the millisecond level. At the same time, we can use the Difftime () function, but it is only accurate to seconds. The function is defined as follows:
Double Difftime (time_t time1, time_t TIME0);
Although the time interval in seconds that the function returns is double, this does not mean that the time has the same precision as the double, which is determined by its arguments (time_t is measured in seconds). For example, the following procedure:
#include <time.h> #include <stdio.h> #include <stdlib.h> int main (void) {time_t start,end; Start = time (NULL); System ("pause"); End = Time (NULL); printf ("The pause used%f seconds.\n", Difftime (End,start)); System ("pause"); return 0; } |
&NBSP
runs as follows:
Press any key to continue ...
The pause used 2.000000 seconds.
Press any key to continue ...
It can be imagined that the pause time is not so fortuitous for exactly 2 seconds. In fact, you will replace the line with the "//<-" annotation on the above program with one line of code:
printf ("The pause used%f seconds./n", End-start);
The
runs the same result.