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