RUSH
Reusable Utilities in Single Headers is a collection of header-only utilities for C++
Loading...
Searching...
No Matches
chrono.hpp
Go to the documentation of this file.
1
8#ifndef RUSH_CHRONO_HPP
9#define RUSH_CHRONO_HPP
10
11#include <chrono>
12#include <iostream>
13#include <ratio>
14#include <string>
15#include <thread>
16#include <utility>
17
18namespace rush::chrono {
19
21#define DEFINE_UNIT_MACRO(name, unit) \
22 template <> \
23 struct Unit<name> { \
24 static constexpr const char *str() { \
25 return unit; \
26 } \
27 };
28
29template <typename T>
30struct Unit;
31
32DEFINE_UNIT_MACRO(std::nano, "ns")
33DEFINE_UNIT_MACRO(std::micro, "us")
34DEFINE_UNIT_MACRO(std::milli, "ms")
35DEFINE_UNIT_MACRO(std::ratio<1>, "s")
36DEFINE_UNIT_MACRO(std::ratio<60>, "min")
37DEFINE_UNIT_MACRO(std::ratio<3600>, "h")
38DEFINE_UNIT_MACRO(std::ratio<86400>, "day")
41using ns = std::nano;
42using us = std::micro;
43using ms = std::milli;
44using s = std::ratio<1>;
45using min = std::ratio<60>;
46using hour = std::ratio<3600>;
47using day = std::ratio<86400>;
48
55template <typename T = s>
56inline void sleep(double t) {
57 std::this_thread::sleep_for(std::chrono::duration<double, T>(t));
58}
59
65template <typename T = s>
66class Chrono {
67public:
68 Chrono() : t0_(std::chrono::high_resolution_clock::now()) {}
69 ~Chrono() = default;
70 Chrono(const Chrono &) = delete;
71 Chrono(Chrono &&) noexcept = delete;
72 Chrono &operator=(const Chrono &) = delete;
73 Chrono &operator=(Chrono &&other) noexcept = delete;
74
79 inline void tic() {
80 t0_ = std::chrono::high_resolution_clock::now();
81 }
82
88 [[nodiscard]] inline double toc() const {
89 return std::chrono::duration_cast<std::chrono::duration<double, T>>(std::chrono::high_resolution_clock::now() - t0_).count();
90 }
91
97 [[nodiscard]] inline std::string unit() const {
98 return Unit<T>().str();
99 }
100
101private:
102 std::chrono::time_point<std::chrono::high_resolution_clock> t0_;
103};
104
110template <typename T = s>
111class Chronometer : public Chrono<T> {
112public:
118 explicit Chronometer(std::string name = "") : name_{std::move(name)} {}
119
120 ~Chronometer() {
121 double t = Chrono<T>::toc();
122 if(!name_.empty()) {
123 std::cout << "[" << name_ << "] ";
124 }
125 std::cout << "Elapsed time: " << t << " " << Chrono<T>::unit() << std::endl;
126 }
127
128 Chronometer(const Chronometer &) = delete;
129 Chronometer(Chronometer &&) noexcept = delete;
130 Chronometer &operator=(const Chronometer &) = delete;
131 Chronometer &operator=(Chronometer &&other) noexcept = delete;
132
133private:
134 std::string name_;
135};
136
137} // namespace rush::chrono
138
139#endif // RUSH_CHRONO_HPP
std::ratio< 86400 > day
Convenience alias for days.
Definition chrono.hpp:47
std::ratio< 3600 > hour
Convenience alias for hours.
Definition chrono.hpp:46
std::milli ms
Convenience alias for milliseconds.
Definition chrono.hpp:43
std::micro us
Convenience alias for microseconds.
Definition chrono.hpp:42
std::nano ns
Convenience alias for nanoseconds.
Definition chrono.hpp:41
std::ratio< 60 > min
Convenience alias for minutes.
Definition chrono.hpp:45
std::ratio< 1 > s
Convenience alias for seconds.
Definition chrono.hpp:44
A simple chrono class to measure elapsed time.
Definition chrono.hpp:66
std::string unit() const
Get unit as string.
Definition chrono.hpp:97
double toc() const
Get elapsed time since last tic.
Definition chrono.hpp:88
void tic()
Reset the chrono timer.
Definition chrono.hpp:79
A chrono class that automatically prints elapsed time.
Definition chrono.hpp:111
Chronometer(std::string name="")
Constructor.
Definition chrono.hpp:118