26 namespace Timekeeper {
32 template <
typename T = std::chrono::system_clock>
39 Stopwatch(std::string name =
"stopwatch",
bool running =
false)
83 typename T::duration
Get()
const
96 using namespace std::chrono;
99 typedef typename TClock::period TPeriod;
100 if (ratio_less_equal<TPeriod, micro>::value) {
101 microseconds d = duration_cast<microseconds>(
Get());
103 }
else if (ratio_less_equal<TPeriod, milli>::value) {
104 milliseconds d = duration_cast<milliseconds>(
Get());
107 seconds d = duration_cast<seconds>(
Get());
123 template <
typename T>
124 std::ostream& operator<<(std::ostream& oss, Stopwatch<T>
const& stopwatch)
126 return (oss << stopwatch.ToString());
std::string ToString() const
Returns string representation of readout.
Stopwatch(std::string name="stopwatch", bool running=false)
Constructor initializes stopwatch.
T::duration m_accumulated
T::time_point m_last_start
Utilities to tag clocks and reformat clock readout to string.
std::string GetName() const
Return name of this stopwatch.
Stopwatch & Reset()
Resets stopwatch i.e.
T::duration Get() const
Returns the accumulated value without altering the stopwatch state.
Stopwatch & Stop()
Stops the stopwatch if it was running.
static std::string ToColonString(std::chrono::seconds d)
Procude string in hh:mm:ss format.
bool IsRunning() const
Reports whether stopwatch has been started.
Stopwatch & Start()
Starts stopwatch if it was stopped.
Provides a stopwatch interface to time: it accumulates time between start/stop pairs.