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 the stopwatch specified by the
stopWatch data structure:
The following line returns the number of milliseconds that the stopwatch
specified by the
stopWatch data structure has been running:
The following line stops the stopwatch specified by the
stopWatch data structure without resetting it:
The following line restarts a stopwatch specified by the
stopWatch data structure. Nothing will happen if the stopwatch
is already running:
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. The source code repository is available on GitHub. I recommend that you checkout the latest revision of the master branch, unless you're looking for something specific.
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: email@example.comHome
Last updated on January 6, 2019