A Simple POSIX Stopwatch Library
by Michael Dipperstein
I periodically need to measure time intervals like the time between events or the time it takes for an event to complete, and I find myself periodically writing some kind of timer function. After a couple hundred times, I finally got the clue and realized that I should just write a stopwatch library and reuse the library. Now that I have a library, I'll probably never need to time anything again.
I've decided to publish my library to improve the chances of somebody finding it useful. Even if that somebody isn't me.
If you've seen my other libraries, you should have noticed that I write a lot of ANSI C code. It's probably the lowest common denominator. Unfortunately, I don't know of any reliable ANSI C time functions. POSIX has some time functions, and there are a lot of compilers that support POSIX, including several ports of GCC. If you're still stuck using MS Windows, mingw provides a Windows porting of GCC.
I've also included a C++ class that wraps my library functions. If you happen to be more attached to C++ than C, I think you'll find the C++ wrappers to your liking.
I think my stopwatch library code is pretty much self explanatory. Here's an explanation of it's usage just in case it isn't.
The following line declares a stopwatch data structure
The following line starts or resets a stopwatch using the data structure
The following line returns the number of milliseconds a stopwatch using
the data structure
stopWatch has been running:
The following line stops a stopwatch using the data structure
stopWatch without resetting it:
The following line restarts a stopwatch using the data structure
stopWatch. Nothing will happen if the stopwatch is already
The following line declares and initializes a stopwatch object
The following line starts or resets a stopwatch object
The following line returns the number of milliseconds a stopwatch object
stopWatch has been running:
The following line stops a stopwatch object
without resetting it:
The following line restarts a stopwatch object
nothing will happen if the stopwatch is already running:
I am releasing my stopwatch library under the LGPL. As I add enhancements or fix bugs, I will post them here as newer versions. The larger the version number, the newer the version. I will retain the older versions for historical reasons. I recommend that most people download the newest version unless there is a compelling reason to do otherwise.
Each version is contained in its own zipped archive which includes the source files and brief instructions for building an executable. None of the archives contain executable programs. A copy of the archives may be obtained by clicking on the links below.
|Version 0.2||Replaces gettimeofday() with clock_gettime() to be resilient to time adjustments.|
|Version 0.1||Initial release.|
All the source code that I have provided is written in POSIX C/C++. I would expect it to build correctly on any machine with a POSIX C compiler. Microsoft Visual Studio is not POSIX compliant. I have tested the code compiled with gcc on Linux and mingw on Windows XP.
If you have any further questions or comments, you may contact me by e-mail. My e-mail address is: firstname.lastname@example.org