ROSE 0.11.145.192
Stopwatch.h
1// WARNING: Changes to this file must be contributed back to Sawyer or else they will
2// be clobbered by the next update from Sawyer. The Sawyer repository is at
3// https://gitlab.com/charger7534/sawyer.git.
4
5
6
7
8#ifndef Sawyer_Stopwatch_H
9#define Sawyer_Stopwatch_H
10
11#include <Sawyer/Sawyer.h>
12
13#ifdef SAWYER_HAVE_BOOST_CHRONO
14# include <boost/chrono/duration.hpp>
15# include <boost/chrono/system_clocks.hpp>
16#endif
17
18namespace Sawyer {
19
41class SAWYER_EXPORT Stopwatch {
42public:
43#ifdef SAWYER_HAVE_BOOST_CHRONO
44 typedef boost::chrono::high_resolution_clock::time_point TimePoint;
45 typedef boost::chrono::duration<double> Duration;
46#else
47 typedef double TimePoint;
48 typedef double Duration;
49#endif
50
51private:
52#include <Sawyer/WarningsOff.h>
53 mutable TimePoint begin_ = 0.0; // time that this stopwatch (re)started
54 mutable Duration elapsed_ = 0.0; // sum of elapsed run time in seconds
55 bool running_ = false; // state of the stopwatch: running or not
56#include <Sawyer/WarningsRestore.h>
57
58public:
62 explicit Stopwatch(bool start=true) {
63 start && this->start();
64 }
65
69 double clear(double value=0.0);
70
77 double start();
78 double start(double value);
84 double restart();
85
91 double stop(bool clear=false);
92
97 double report(bool clear=false) const;
98
102 bool isRunning() const { return running_; }
103
105 std::string toString() const;
106
108 static std::string toString(double seconds);
109};
110
111
112SAWYER_EXPORT std::ostream& operator<<(std::ostream&, const Stopwatch&);
113
114} // namespace
115#endif
Simple elapsed time.
Definition Stopwatch.h:41
double clear(double value=0.0)
Stop and reset the timer to the specified value.
double report(bool clear=false) const
Report current accumulated time without stopping or starting.
double start(double value)
Start the timer and report accumulated time.
static std::string toString(double seconds)
Format time in human readable manner.
bool isRunning() const
Query state of stopwatch.
Definition Stopwatch.h:102
double stop(bool clear=false)
Stop the timer and report accumulated time.
double restart()
Restart the timer.
std::string toString() const
Convert a stopwatch to a human-readalbe string.
double start()
Start the timer and report accumulated time.
Stopwatch(bool start=true)
Construct and optionally start a timer.
Definition Stopwatch.h:62
Sawyer support library.