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.


Library Usage

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.

C Usage

The following line declares a stopwatch data structure stopWatch:
stopwatch_t stopWatch;

The following line starts or resets the stopwatch specified by the stopWatch data structure:
StartTimer(&stopWatch);

The following line returns the number of milliseconds that the stopwatch specified by the stopWatch data structure has been running:
ReadTimer(&stopWatch);

The following line stops the stopwatch specified by the stopWatch data structure without resetting it:
StopTimer(&stopWatch);

The following line restarts a stopwatch specified by the stopWatch data structure. Nothing will happen if the stopwatch is already running:
ResumeTimer(&stopWatch);

C++ Usage

The following line declares and initializes a stopwatch object stopWatch:
stopwatch_c stopWatch;

The following line starts or resets a stopwatch object stopWatch:
stopWatch.Start();

The following line returns the number of milliseconds a stopwatch object stopWatch has been running:
stopWatch.Read();

The following line stops a stopwatch object stopWatch without resetting it:
stopWatch.Stop();

The following line restarts a stopwatch object stopWatch. Nothing will happen if the stopwatch is already running:
stopWatch.Resume();

Actual Software

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.

Repository Location https://github.com/michaeldipperstein/stopwatch

Portability

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: mdipperstein@gmail.com

Home

Last updated on January 6, 2019