-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChronoStopwatch.cpp
96 lines (76 loc) · 1.75 KB
/
ChronoStopwatch.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
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "ChronoStopwatch.h"
ChronoStopwatch::ChronoStopwatch()
{
}
ChronoStopwatch::~ChronoStopwatch()
{
}
size_t ChronoStopwatch::start()
{
mStart.push_back(std::chrono::high_resolution_clock::now());
return mStart.size() - 1;
}
double ChronoStopwatch::stop()
{
size_t index = mStart.size() - 1;
for (size_t x = mStop.size() - 1; mStop.size() > 0 && x >= 0; x--)
{
if (mStop[x].second == index)
{
if (index > 0)
{
index--;
x = mStop.size();
}
else
return -1.0;
}
if (x == 0)
break;
}
return this->stop(index);
}
double ChronoStopwatch::stop(size_t i)
{
mStop.push_back(timepointIndex(std::chrono::high_resolution_clock::now(), i));
return this->getDuration(i);
}
double ChronoStopwatch::getDuration(size_t i) const
{
timepointIndex endTime;
bool found = false;
for (size_t x = 0; x < mStop.size(); x++)
{
if (mStop[x].second == i)
{
endTime = mStop[x];
found = true;
break;
}
}
if (found)
{
auto d = std::chrono::duration_cast<std::chrono::duration<double>>(endTime.first - mStart[i]);
return d.count();
}
else
return -1.0;
}
std::string ChronoStopwatch::getFormatString(double nmbr)
{
std::stringstream test;
std::chrono::duration<double> diff(nmbr);
auto hours = std::chrono::duration_cast<std::chrono::hours>(diff);
diff -= hours;
auto mins = std::chrono::duration_cast<std::chrono::minutes>(diff);
diff -= mins;
auto secs = std::chrono::duration_cast<std::chrono::seconds>(diff);
diff -= secs;
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(diff);
test << std::setfill('0');
test << std::setw(2) << hours.count() << ':'
<< std::setw(2) << mins.count() << ':'
<< std::setw(2) << secs.count() << '.'
<< std::setw(3) << millis.count();
return test.str();
}