// WARNING: Changes to this file must be contributed back to Sawyer or else they will // be clobbered by the next update from Sawyer. The Sawyer repository is at // https://github.com/matzke1/sawyer. #ifndef Sawyer_Stopwatch_H #define Sawyer_Stopwatch_H #include #include "Sawyer.h" #ifdef SAWYER_HAVE_BOOST_CHRONO #include #endif namespace Sawyer { /** Simple elapsed time. * * @code * Stopwatch stopwatch; // starts immediately unless false argument is given * do_something(); * std::cerr <<"that took " < Duration; #else typedef double TimePoint; typedef double Duration; #endif private: #include "../util/WarningsOff.h" mutable TimePoint begin_; // time that this stopwatch (re)started mutable Duration elapsed_; // sum of elapsed run time in seconds bool running_; // state of the stopwatch: running or not #include "../util/WarningsRestore.h" public: /** Construct and optionally start a timer. * * The timer is started immediately unless the constructor is invoked with a false argument. */ explicit Stopwatch(bool start=true): elapsed_(0), running_(false) { start && this->start(); } /** Stop and reset the timer to the specified value. * * The timer is stopped if it is running. Returns the accumulated time before resetting it to the specified value. */ double clear(double value=0.0); /** Start the timer and report accumulated time. * * Reports the time accumulated as of this call and makes sure the clock is running. If a value is specified then the * stopwatch starts with that much time accumulated. * * @{ */ double start(); double start(double value); double start(bool b) SAWYER_DEPRECATED("use start() or start(double)") { return b ? start() : start(0.0); } /** @} */ /** Restart the timer. * * Reports the accumulated time as of this call, then resets it to zero and starts or restarts the clock running. */ double restart(); /** Stop the timer and report accumulated time. * * If the timer is running then it is stopped and the elapsed time is accumulated, otherwise the timer * remains in the stopped state an no additional time is accumulated. If @p clear is set, then the accumlated time is * reset to zero. In any case, the return value is the accumulated time before being optionally reset to zero. */ double stop(bool clear=false); /** Report current accumulated time without stopping or starting. * * If @p clear is set then the accumulated time is reset to zero without affecting the return value or the running state, * and the stopwatch starts reaccumulating time as of this call. */ double report(bool clear=false) const; /** Query state of stopwatch. * * Returns true if and only if the stopwatch is running. */ bool isRunning() const { return running_; } }; SAWYER_EXPORT std::ostream& operator<<(std::ostream&, const Stopwatch&); } // namespace #endif