-
Notifications
You must be signed in to change notification settings - Fork 0
/
BbStopWatch.cpp
83 lines (65 loc) · 1.53 KB
/
BbStopWatch.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "BbStopWatch.hpp"
#include <utility>
using namespace std;
void BbStopWatch::start()
{
if (!isRunning)
{
splitTime = startTime = hr_clock::now();
isRunning = true;
}
}
void BbStopWatch::stop()
{
if (isRunning)
{
stopTime = hr_clock::now();
isRunning = false;
}
}
void BbStopWatch::reset()
{
stopTime = splitTime = startTime = hr_clock::now();
isRunning = false;
}
void BbStopWatch::restart()
{
reset();
start();
}
double BbStopWatch::get_duration(const hr_t_point& e, const hr_t_point& s, const TimeUOM& p) const
{
using namespace chrono;
switch (p)
{
case TimeUOM::hours:
return duration_cast<duration<double, std::ratio<3600>>>(e - s).count();
case TimeUOM::minutes:
return duration_cast<duration<double, std::ratio<60>>>(e - s).count();
case TimeUOM::seconds:
return duration_cast<duration<double, std::ratio<1>>>(e - s).count();
case TimeUOM::milliseconds:
return duration_cast<duration<double, std::milli>>(e - s).count();
case TimeUOM::microseconds:
return duration_cast<duration<double, std::micro>>(e - s).count();
case TimeUOM::nanoseconds:
return duration_cast<duration<double, std::nano>>(e - s).count();
}
}
double BbStopWatch::split()
{
if (!isRunning)
{
return 0.;
}
auto now = hr_clock::now();
double duration = get_duration(now, splitTime, period);
splitTime = std::move(now);
return duration;
}
double BbStopWatch::get_total_running_time() const
{
auto now = isRunning ? hr_clock::now() : stopTime;
double duration = get_duration(now, startTime, period);
return duration;
}