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 101 : StopWatch::StopWatch(bool start)
10 101 : : m_stopped(false)
11 : {
12 101 : if (start)
13 : {
14 101 : this->start();
15 : }
16 101 : }
17 :
18 101 : void StopWatch::start()
19 : {
20 101 : m_stopped = false;
21 101 : m_started = system_clock::now();
22 101 : }
23 :
24 89 : void StopWatch::stop()
25 : {
26 89 : m_finished = system_clock::now();
27 89 : m_stopped = true;
28 89 : }
29 :
30 89 : long long StopWatch::getElapsedMilliseconds() const
31 : {
32 89 : return duration_cast<milliseconds>(getElapsedDuration()).count();
33 : }
34 :
35 89 : double StopWatch::getElapsedSeconds() const
36 : {
37 89 : return getElapsedMilliseconds() / MS_PER_SECOND;
38 : }
39 :
40 89 : std::string StopWatch::getElapsedSecondsString(const std::string& suffix /*= ""*/) const
41 : {
42 89 : std::stringstream ss;
43 89 : ss << std::fixed << std::setprecision(3) << getElapsedSeconds() << suffix;
44 178 : return ss.str();
45 89 : }
46 :
47 89 : system_clock::duration StopWatch::getElapsedDuration() const
48 : {
49 : // if stop() hasn't been called yet, use the current time
50 89 : if (m_stopped) return m_finished - m_started;
51 0 : else return system_clock::now() - m_started;
52 : }
53 : }
|