From: <sv...@ww...> - 2004-09-30 07:20:56
|
Author: mkrose Date: 2004-09-30 00:20:48 -0700 (Thu, 30 Sep 2004) New Revision: 1265 Modified: trunk/CSP/SimData/CHANGES.current trunk/CSP/SimData/Include/SimData/Timing.h Log: Add a timer class to wrap get_realtime with basic stopwatch functions. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1265 Modified: trunk/CSP/SimData/CHANGES.current =================================================================== --- trunk/CSP/SimData/CHANGES.current 2004-09-30 07:19:09 UTC (rev 1264) +++ trunk/CSP/SimData/CHANGES.current 2004-09-30 07:20:48 UTC (rev 1265) @@ -4,6 +4,9 @@ 2004-09-29: onsight * Clean up warnings and formatting in Date.h/cpp. + * Add a timer class to wrap get_realtime with basic stopwatch + functions. + 2004-09-28: delta * Added a #undef ERROR after inlcuding windows.h, protected behind a #ifdef. Modified: trunk/CSP/SimData/Include/SimData/Timing.h =================================================================== --- trunk/CSP/SimData/Include/SimData/Timing.h 2004-09-30 07:19:09 UTC (rev 1264) +++ trunk/CSP/SimData/Include/SimData/Timing.h 2004-09-30 07:20:48 UTC (rev 1265) @@ -64,6 +64,54 @@ timing_t get_realtime(); +/** A simple timing class with standard stopwatch functions. The + * timing precision is equivalent to get_realtime(). + */ +class Timer { + timing_t _start; + timing_t _elapsed; + bool _running; + +public: + /// Construct a new timer. + Timer(): _start(0), _elapsed(0), _running(false) { } + + /// Reset the elapsed time to zero and start the timer running. + inline void start() { + _elapsed = 0.0; + resume(); + } + + /// Resumes counting without reseting the elapsed time. + inline void resume() { + if (!_running) { + _start = get_realtime(); + _running = true; + } + } + + /// Stop the timer. + inline timing_t stop() { + if (_running) { + _elapsed += get_realtime() - _start; + _running = false; + } + return _elapsed; + } + + /// Return true if the timer is running. + inline bool running() const { return _running; } + + /// Get the total elasped time. + inline timing_t elapsed() const { + return (_running ? get_realtime() - _start + _elapsed : _elapsed); + } + + /// Reset the elapsed time to zero (does not stop the timer). + inline void reset() { _elapsed = 0; } +}; + + ///////////////////////////////////////////////////////////// // end of timing routines |