Appendix A: The Timer Module

[ Home ] [ Previous Page ] [ Next Page ]
[ References ]

[Timer Module Source File: timer.c ]
[Timer Module Public Header File: timer.h ]
[Timer Module Private Header File: timerp.h ]

This is a simple module that measures the elapsed time, in milliseconds, between two events. A timer ID must first be obtained by calling TIMER_create; when the ID is no longer needed it must be destroyed by calling TIMER_destroy. Events are signaled by invoking one of the methods TIMER_start, TIMER_restart, TIMER_stop, TIMER_get_elapsed_secs or TIMER_get_elapsed_msecs.

Timer_start
This method sets the elapsed time to 0 and signals an initiating event.
TIMER_stop
This method signals a terminal event.
TIMER_restart
This method restarts the timer after it has been stopped without resetting the elapsed time. If the timer has not been started and stopped invoking it is the same as invoking TIMER_start.
TIMER_get_elapsed_secs, TIMER_get_elapsed_msecs
These methods return the elapsed time in seconds and milliseconds, respectively, between the last pair of initiating and terminating events. If there has not been a terminating event, these methods return the elapsed time between the last initiating event and the time of invocation; they do not stop the timer or reset the elapsed time.

The module uses the POSIX timeb facility to record elapsed time in milliseconds. The timeb facility includes the struct timeb type and the ftime function; in Microsoft Windows XP™ these are given the names struct _timeb and _ftime. Conditionally compiled code in timerp.h maps the Windows names to the standard POSIX names:

#ifdef _WIN32
#define ftime _ftime
#define timeb _timeb
#endif

Note that the macro _WIN32 is automatically defined whenever you compile code with a WIN32 compiler, such as that provided with Microsoft Visual Studio .NET™.

This module has been tested in both Microsoft Windows XP™ and Linux. It should function correctly in any fairly recent Windows or Unix environment. The complete code for the timer module can be found in timer.c , timer.h and timerp.h.

Next: Appendix B: The Rand Module


Copyright © 2005 by Jack Straub
jstraub@centurytel.net