-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chrono.hpp
100 lines (80 loc) · 2.98 KB
/
Chrono.hpp
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
97
98
99
100
// Copyright (c) 2022 Francesco Cavaliere
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef CAV_INCLUDE_CAV_CHRONO_HPP
#define CAV_INCLUDE_CAV_CHRONO_HPP
#ifndef CAV_INCLUDEs_CHRONO_HPP
#define CAV_INCLUDEs_CHRONO_HPP
#include <chrono>
#include "../comptime/test.hpp"
namespace cav {
using nsec = std::chrono::nanoseconds;
using usec = std::chrono::microseconds;
using msec = std::chrono::milliseconds;
using sec = std::chrono::seconds;
using minutes = std::chrono::minutes;
using hours = std::chrono::hours;
using days = std::chrono::days;
using weeks = std::chrono::weeks;
using months = std::chrono::months;
using years = std::chrono::years;
template <typename UnitT = usec>
struct Chrono {
using high_res_clock = std::chrono::high_resolution_clock;
using time_point = std::chrono::system_clock::time_point;
time_point start;
time_point last;
Chrono()
: start(high_res_clock::now())
, last(start) {
}
template <typename UnitT2>
[[nodiscard]] static constexpr double time_cast(double t) {
using factor = std::ratio_divide<typename UnitT::period, typename UnitT2::period>;
return t * factor::num / factor::den;
}
void restart() {
start = last = high_res_clock::now();
}
uint64_t lap() {
auto old_last = last;
last = high_res_clock::now();
return std::chrono::duration_cast<UnitT>(last - old_last).count();
}
[[nodiscard]] uint64_t from_start() const {
auto now = high_res_clock::now();
return std::chrono::duration_cast<UnitT>(now - start).count();
}
template <typename UnitT2>
[[nodiscard]] auto from_start() const {
return time_cast<UnitT2>(from_start());
}
template <typename UnitT2>
[[nodiscard]] auto lap() {
return time_cast<UnitT2>(lap());
}
};
#ifdef CAV_COMP_TESTS
namespace {
CAV_PASS(Chrono<nsec>::template time_cast<usec>(1) == 1e-3);
CAV_PASS(Chrono<usec>::template time_cast<usec>(1) == 1.0);
CAV_PASS(Chrono<msec>::template time_cast<usec>(1) == 1000.0);
CAV_PASS(Chrono<sec>::template time_cast<usec>(1) == 1000000.0);
CAV_PASS(Chrono<sec>::template time_cast<days>(86400) == 1.0);
CAV_PASS(Chrono<days>::template time_cast<sec>(1) == 86400.0);
} // namespace
#endif
} // namespace cav
#endif /* CAV_INCLUDE_CHRONO_HPP */
#endif /* CAV_INCLUDE_CAV_CHRONO_HPP */