Blog Archive

Wednesday, November 10, 2010

[C Language] Use of the ftime() Function

Use of the ftime() Function



Program FTIME.C illustrates how to continually fetch the time of the system clock so as perform a task for a specified period of time. In this simple example, a dot is printed for 5000 ms. Struct timeb consists of, among other things, a long integer, "time" which is the number of seconds since Jan 1, 70 and a short integer, "millitm", which is the fractional number of milliseconds.

At the beginning of the task, line 9, the start time is fetched. The task is then performed, in this case, the simple printing of a dot and the current time is fetched off the system clock in line 14. This is repeated until the current time is 5000 ms larger than the start time.

The advantage of this approach over using the TurboC delay function is that while executing the delay function, nothing else is being done. Thus, although the delay function has its place, it is unusable in many applications, where something dynamic must be done for a specified period of time.

/*
** Program FTIME.C
**
** Illustrates the use of ftime function to perform a task for a period
** of time. Prints dots for 5000 msecs.
**
** H. Paul Roach, MSU, 5 August, '96
*/

#include /* 1 */
#include /* 2 */
#include /* 3 */
/* 4 */
void main(void) /* 5 */
{ /* 6 */
struct timeb t_start, t_current; /* 7 */
int t_diff; /* 8 */
ftime(&t_start); /* 9 */
do /* 10 */
{ /* 11 */
printf("."); /* 12 */
ftime(&t_current); /* 13 */
t_diff = (int) (1000.0 * (t_current.time - t_start.time)/* 14 */
+ (t_current.millitm - t_start.millitm)); /* 15 */
} /* 16 */
while(t_diff < 5000); /* 17 */
} /* 18 */

No comments:

Post a Comment