RUSH
Reusable Utilities in Single Headers is a collection of header-only utilities for C++
Toggle main menu visibility
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
18
namespace
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
29
template
<
typename
T>
30
struct
Unit;
31
32
DEFINE_UNIT_MACRO(std::nano,
"ns"
)
33
DEFINE_UNIT_MACRO(std::micro,
"us"
)
34
DEFINE_UNIT_MACRO(std::milli,
"ms"
)
35
DEFINE_UNIT_MACRO(std::ratio<1>,
"s"
)
36
DEFINE_UNIT_MACRO(std::ratio<60>,
"min"
)
37
DEFINE_UNIT_MACRO(std::ratio<3600>,
"h"
)
38
DEFINE_UNIT_MACRO(std::ratio<86400>,
"day"
)
40
41
using
ns
= std::nano;
42
using
us
= std::micro;
43
using
ms
= std::milli;
44
using
s
= std::ratio<1>;
45
using
min
= std::ratio<60>;
46
using
hour
= std::ratio<3600>;
47
using
day
= std::ratio<86400>;
48
55
template <typename T =
s
>
56
inline
void
sleep
(
double
t) {
57
std::this_thread::sleep_for(std::chrono::duration<double, T>(t));
58
}
59
65
template
<
typename
T = s>
66
class
Chrono {
67
public
:
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
101
private
:
102
std::chrono::time_point<std::chrono::high_resolution_clock> t0_;
103
};
104
110
template
<
typename
T = s>
111
class
Chronometer
:
public
Chrono<T> {
112
public
:
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
133
private:
134
std::
string
name_;
135
};
136
137
}
// namespace rush::chrono
138
139
#endif
// RUSH_CHRONO_HPP
rush::chrono::sleep
void sleep(double t)
Sleep for the specified duration.
Definition
chrono.hpp:56
rush::chrono::day
std::ratio< 86400 > day
Convenience alias for days.
Definition
chrono.hpp:47
rush::chrono::hour
std::ratio< 3600 > hour
Convenience alias for hours.
Definition
chrono.hpp:46
rush::chrono::ms
std::milli ms
Convenience alias for milliseconds.
Definition
chrono.hpp:43
rush::chrono::us
std::micro us
Convenience alias for microseconds.
Definition
chrono.hpp:42
rush::chrono::ns
std::nano ns
Convenience alias for nanoseconds.
Definition
chrono.hpp:41
rush::chrono::min
std::ratio< 60 > min
Convenience alias for minutes.
Definition
chrono.hpp:45
rush::chrono::s
std::ratio< 1 > s
Convenience alias for seconds.
Definition
chrono.hpp:44
rush::chrono::Chrono::unit
std::string unit() const
Get unit as string.
Definition
chrono.hpp:97
rush::chrono::Chrono::toc
double toc() const
Get elapsed time since last tic.
Definition
chrono.hpp:88
rush::chrono::Chrono::tic
void tic()
Reset the chrono timer.
Definition
chrono.hpp:79
rush::chrono::Chronometer
A chrono class that automatically prints elapsed time.
Definition
chrono.hpp:111
rush::chrono::Chronometer::Chronometer
Chronometer(std::string name="")
Constructor.
Definition
chrono.hpp:118
include
rush
chrono.hpp
Generated by
1.17.0