18namespace rush::chrono {
21#define DEFINE_UNIT_MACRO(name, unit) \
24 static constexpr const char *str() { \
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")
44using
s = std::ratio<1>;
45using
min = std::ratio<60>;
46using
hour = std::ratio<3600>;
47using
day = std::ratio<86400>;
55template <typename T =
s>
56inline
void sleep(
double t) {
57 std::this_thread::sleep_for(std::chrono::duration<double, T>(t));
65template <
typename T = s>
68 Chrono() : t0_(std::chrono::high_resolution_clock::now()) {}
80 t0_ = std::chrono::high_resolution_clock::now();
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();
97 [[nodiscard]]
inline std::string
unit()
const {
98 return Unit<T>().str();
102 std::chrono::time_point<std::chrono::high_resolution_clock> t0_;
110template <
typename T = s>
118 explicit Chronometer(std::string name =
"") : name_{std::move(name)} {}
123 std::cout <<
"[" << name_ <<
"] ";
125 std::cout <<
"Elapsed time: " << t <<
" " <<
Chrono<T>::unit() << std::endl;
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