Line data Source code
1 : #include "StopWatch.h"
2 : #include <sstream>
3 : #include <iomanip>
4 :
5 : using namespace std::chrono;
6 :
7 : namespace Utils
8 : {
9 104 : StopWatch::StopWatch(bool start)
10 104 : : m_stopped(false)
11 : {
12 104 : if (start)
13 : {
14 104 : this->start();
15 : }
16 104 : }
17 :
18 104 : void StopWatch::start()
19 : {
20 104 : m_stopped = false;
21 104 : m_started = system_clock::now();
22 104 : }
23 :
24 92 : void StopWatch::stop()
25 : {
26 92 : m_finished = system_clock::now();
27 92 : m_stopped = true;
28 92 : }
29 :
30 92 : long long StopWatch::getElapsedMilliseconds() const
31 : {
32 92 : return duration_cast<milliseconds>(getElapsedDuration()).count();
33 : }
34 :
35 92 : double StopWatch::getElapsedSeconds() const
36 : {
37 92 : return getElapsedMilliseconds() / MS_PER_SECOND;
38 : }
39 :
40 92 : std::string StopWatch::getElapsedSecondsString(const std::string& suffix /*= ""*/) const
41 : {
42 92 : std::stringstream ss;
43 92 : ss << std::fixed << std::setprecision(3) << getElapsedSeconds() << suffix;
44 184 : return ss.str();
45 92 : }
46 :
47 92 : system_clock::duration StopWatch::getElapsedDuration() const
48 : {
49 : // if stop() hasn't been called yet, use the current time
50 92 : if (m_stopped) return m_finished - m_started;
51 0 : else return system_clock::now() - m_started;
52 : }
53 : }
|