11#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5 && __GNUC_MINOR__ < 8
12#define _GLIBCXX_USE_NANOSLEEP
28 using clock = std::chrono::steady_clock;
34 using time_print_t = std::function<std::string(std::string, std::string)>;
50 static std::string
Simple(std::string title, std::string time) {
return title +
": " + time; }
53 static std::string
Big(std::string title, std::string time) {
54 return std::string(
"-----------------------------------------\n") +
"| " + title +
" | Time = " + time +
"\n" +
55 "-----------------------------------------";
64 std::string
time_it(std::function<
void()> f,
double target_time = 1) {
72 std::chrono::duration<double> elapsed = clock::now() -
start_;
73 total_time = elapsed.count();
74 }
while(n++ < 100u && total_time < target_time);
76 std::string out =
make_time_str(total_time /
static_cast<double>(n)) +
" for " + std::to_string(n) +
" tries";
84 std::chrono::duration<double> elapsed = stop -
start_;
85 double time = elapsed.count() /
static_cast<double>(
cycles);
92 auto print_it = [](
double x, std::string unit) {
93 const unsigned int buffer_length = 50;
94 std::array<char, buffer_length> buffer;
95 std::snprintf(buffer.data(), buffer_length,
"%.5g", x);
96 return buffer.data() + std::string(
" ") + unit;
100 return print_it(time * 1000000000,
"ns");
102 return print_it(time * 1000000,
"us");
104 return print_it(time * 1000,
"ms");
106 return print_it(time,
"s");
std::ostream & operator<<(std::ostream &in, const CLI::Timer &timer)
This prints out the time if shifted into a std::cout like stream.
Definition Timer.hpp:134
This class prints out the time upon destruction.
Definition Timer.hpp:121
AutoTimer(std::string title="Timer", time_print_t time_print=Simple)
Reimplementing the constructor is required in GCC 4.7.
Definition Timer.hpp:124
~AutoTimer()
This destructor prints the string.
Definition Timer.hpp:128
This is a simple timer with pretty printing. Creating the timer starts counting.
Definition Timer.hpp:25
time_print_t time_print_
This is the function that is used to format most of the timing message.
Definition Timer.hpp:40
Timer(std::string title="Timer", time_print_t time_print=Simple)
Standard constructor, can set title and print function.
Definition Timer.hpp:60
time_point start_
This is the starting point (when the timer was created)
Definition Timer.hpp:43
std::string title_
This is the title of the timer.
Definition Timer.hpp:37
std::string make_time_str() const
This formats the numerical value for the time string.
Definition Timer.hpp:82
std::string to_string() const
This is the main function, it creates a string.
Definition Timer.hpp:111
std::function< std::string(std::string, std::string)> time_print_t
This is the type of a printing function, you can make your own.
Definition Timer.hpp:34
std::size_t cycles
This is the number of times cycles (print divides by this number)
Definition Timer.hpp:46
std::string make_time_str(double time) const
This prints out a time string from a time.
Definition Timer.hpp:91
std::string time_it(std::function< void()> f, double target_time=1)
Time a function by running it multiple times. Target time is the len to target.
Definition Timer.hpp:64
std::chrono::steady_clock clock
This is a typedef to make clocks easier to use.
Definition Timer.hpp:28
Timer & operator/(std::size_t val)
Division sets the number of cycles to divide by (no graphical change)
Definition Timer.hpp:114
static std::string Big(std::string title, std::string time)
This is a fancy print function with — headers.
Definition Timer.hpp:53
std::chrono::time_point< clock > time_point
This typedef is for points in time.
Definition Timer.hpp:31
static std::string Simple(std::string title, std::string time)
Standard print function, this one is set by default.
Definition Timer.hpp:50