CLI11: include/CLI/Timer.hpp Source File (original) (raw)
1
2
3
4
5
6
7#pragma once
8
9
10
11
12
13#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5 && __GNUC_MINOR__ < 8
14#define _GLIBCXX_USE_NANOSLEEP
15#endif
16
17#include
18
19#include
20#include
21#include
22#include
23#include
24#include
25#include
26
27namespace CLI {
28
31 protected:
33 using clock = std::chrono::steady_clock;
34
36 using time_point = std::chrono::time_point;
37
39 using time_print_t = std::function<std::string(std::string, std::string)>;
40
43
46
49
52
53 public:
55 static std::string Simple(std::string title, std::string time) { return title + ": " + time; }
56
58 static std::string Big(std::string title, std::string time) {
59 return std::string("-----------------------------------------\n") + "| " + title + " | Time = " + time + "\n" +
60 "-----------------------------------------";
61 }
62
63 public:
67
69 std::string time_it(std::function<void()> f, double target_time = 1) {
71 double total_time = NAN;
72
73 start_ = clock::now();
74 std::size_t n = 0;
75 do {
76 f();
77 std::chrono::duration elapsed = clock::now() - start_;
78 total_time = elapsed.count();
79 } while(n++ < 100u && total_time < target_time);
80
81 std::string out = make_time_str(total_time / static_cast<double>(n)) + " for " + std::to_string(n) + " tries";
83 return out;
84 }
85
87 std::string make_time_str() const {
89 std::chrono::duration elapsed = stop - start_;
90 double time = elapsed.count() / static_cast<double>(cycles);
92 }
93
94
96 std::string make_time_str(double time) const {
97 auto print_it = [](double x, std::string unit) {
98 const unsigned int buffer_length = 50;
99 std::array<char, buffer_length> buffer;
100 std::snprintf(buffer.data(), buffer_length, "%.5g", x);
101 return buffer.data() + std::string(" ") + unit;
102 };
103
104 if(time < .000001)
105 return print_it(time * 1000000000, "ns");
106 if(time < .001)
107 return print_it(time * 1000000, "us");
108 if(time < 1)
109 return print_it(time * 1000, "ms");
110 return print_it(time, "s");
111 }
112
113
116
122};
123
126 public:
129
130
133};
134
135}
136
138inline std::ostream &operator<<(std::ostream &in, const CLI::Timer &timer) { return in << timer.to_string(); }
This class prints out the time upon destruction.
Definition Timer.hpp:125
AutoTimer(std::string title="Timer", time_print_t time_print=Simple)
Reimplementing the constructor is required in GCC 4.7.
Definition Timer.hpp:128
~AutoTimer()
This destructor prints the string.
Definition Timer.hpp:132
This is a simple timer with pretty printing. Creating the timer starts counting.
Definition Timer.hpp:30
time_print_t time_print_
This is the function that is used to format most of the timing message.
Definition Timer.hpp:45
Timer(std::string title="Timer", time_print_t time_print=Simple)
Standard constructor, can set title and print function.
Definition Timer.hpp:65
time_point start_
This is the starting point (when the timer was created)
Definition Timer.hpp:48
std::string title_
This is the title of the timer.
Definition Timer.hpp:42
std::string make_time_str() const
This formats the numerical value for the time string.
Definition Timer.hpp:87
std::string to_string() const
This is the main function, it creates a string.
Definition Timer.hpp:115
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:39
std::size_t cycles
This is the number of times cycles (print divides by this number)
Definition Timer.hpp:51
std::string make_time_str(double time) const
This prints out a time string from a time.
Definition Timer.hpp:96
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:69
std::chrono::steady_clock clock
This is a typedef to make clocks easier to use.
Definition Timer.hpp:33
Timer & operator/(std::size_t val)
Division sets the number of cycles to divide by (no graphical change)
Definition Timer.hpp:118
static std::string Big(std::string title, std::string time)
This is a fancy print function with — headers.
Definition Timer.hpp:58
std::chrono::time_point< clock > time_point
This typedef is for points in time.
Definition Timer.hpp:36
static std::string Simple(std::string title, std::string time)
Standard print function, this one is set by default.
Definition Timer.hpp:55