CLI11  1.9.0
Timer.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 // Distributed under the 3-Clause BSD License. See accompanying
4 // file LICENSE or https://github.com/CLIUtils/CLI11 for details.
5 
6 // On GCC < 4.8, the following define is often missing. Due to the
7 // fact that this library only uses sleep_for, this should be safe
8 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5 && __GNUC_MINOR__ < 8
9 #define _GLIBCXX_USE_NANOSLEEP
10 #endif
11 
12 #include <array>
13 #include <chrono> // NOLINT(build/c++11)
14 #include <functional>
15 #include <iostream>
16 #include <string>
17 #include <utility>
18 
19 namespace CLI {
20 
22 class Timer {
23  protected:
25  using clock = std::chrono::steady_clock;
26 
28  using time_point = std::chrono::time_point<clock>;
29 
31  using time_print_t = std::function<std::string(std::string, std::string)>;
32 
34  std::string title_;
35 
38 
41 
43  std::size_t cycles{1};
44 
45  public:
47  static std::string Simple(std::string title, std::string time) { return title + ": " + time; }
48 
50  static std::string Big(std::string title, std::string time) {
51  return std::string("-----------------------------------------\n") + "| " + title + " | Time = " + time + "\n" +
52  "-----------------------------------------";
53  }
54 
55  public:
57  explicit Timer(std::string title = "Timer", time_print_t time_print = Simple)
58  : title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {}
59 
61  std::string time_it(std::function<void()> f, double target_time = 1) {
62  time_point start = start_;
63  double total_time;
64 
65  start_ = clock::now();
66  std::size_t n = 0;
67  do {
68  f();
69  std::chrono::duration<double> elapsed = clock::now() - start_;
70  total_time = elapsed.count();
71  } while(n++ < 100u && total_time < target_time);
72 
73  std::string out = make_time_str(total_time / static_cast<double>(n)) + " for " + std::to_string(n) + " tries";
74  start_ = start;
75  return out;
76  }
77 
79  std::string make_time_str() const {
80  time_point stop = clock::now();
81  std::chrono::duration<double> elapsed = stop - start_;
82  double time = elapsed.count() / static_cast<double>(cycles);
83  return make_time_str(time);
84  }
85 
86  // LCOV_EXCL_START
88  std::string make_time_str(double time) const {
89  auto print_it = [](double x, std::string unit) {
90  const unsigned int buffer_length = 50;
91  std::array<char, buffer_length> buffer;
92  std::snprintf(buffer.data(), buffer_length, "%.5g", x);
93  return buffer.data() + std::string(" ") + unit;
94  };
95 
96  if(time < .000001)
97  return print_it(time * 1000000000, "ns");
98  else if(time < .001)
99  return print_it(time * 1000000, "us");
100  else if(time < 1)
101  return print_it(time * 1000, "ms");
102  else
103  return print_it(time, "s");
104  }
105  // LCOV_EXCL_STOP
106 
108  std::string to_string() const { return time_print_(title_, make_time_str()); }
109 
111  Timer &operator/(std::size_t val) {
112  cycles = val;
113  return *this;
114  }
115 };
116 
118 class AutoTimer : public Timer {
119  public:
121  explicit AutoTimer(std::string title = "Timer", time_print_t time_print = Simple) : Timer(title, time_print) {}
122  // GCC 4.7 does not support using inheriting constructors.
123 
125  ~AutoTimer() { std::cout << to_string() << std::endl; }
126 };
127 
128 } // namespace CLI
129 
131 inline std::ostream &operator<<(std::ostream &in, const CLI::Timer &timer) { return in << timer.to_string(); }
CLI::Timer::time_it
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:61
CLI::Timer::title_
std::string title_
This is the title of the timer.
Definition: Timer.hpp:34
CLI::Timer::time_print_t
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:31
CLI::Timer::make_time_str
std::string make_time_str() const
This formats the numerical value for the time string.
Definition: Timer.hpp:79
CLI::AutoTimer::~AutoTimer
~AutoTimer()
This destructor prints the string.
Definition: Timer.hpp:125
CLI::Timer::make_time_str
std::string make_time_str(double time) const
This prints out a time string from a time.
Definition: Timer.hpp:88
CLI::Timer::operator/
Timer & operator/(std::size_t val)
Division sets the number of cycles to divide by (no graphical change)
Definition: Timer.hpp:111
CLI::Timer::time_point
std::chrono::time_point< clock > time_point
This typedef is for points in time.
Definition: Timer.hpp:28
CLI::Timer::start_
time_point start_
This is the starting point (when the timer was created)
Definition: Timer.hpp:40
CLI::detail::to_string
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
Definition: TypeTools.hpp:222
CLI
Definition: App.hpp:27
CLI::Timer::Timer
Timer(std::string title="Timer", time_print_t time_print=Simple)
Standard constructor, can set title and print function.
Definition: Timer.hpp:57
CLI::AutoTimer::AutoTimer
AutoTimer(std::string title="Timer", time_print_t time_print=Simple)
Reimplementing the constructor is required in GCC 4.7.
Definition: Timer.hpp:121
CLI::Timer::time_print_
time_print_t time_print_
This is the function that is used to format most of the timing message.
Definition: Timer.hpp:37
CLI::Timer::clock
std::chrono::steady_clock clock
This is a typedef to make clocks easier to use.
Definition: Timer.hpp:25
CLI::AutoTimer
This class prints out the time upon destruction.
Definition: Timer.hpp:118
CLI::Timer::cycles
std::size_t cycles
This is the number of times cycles (print divides by this number)
Definition: Timer.hpp:43
CLI::Timer::Simple
static std::string Simple(std::string title, std::string time)
Standard print function, this one is set by default.
Definition: Timer.hpp:47
operator<<
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:131
CLI::Timer::Big
static std::string Big(std::string title, std::string time)
This is a fancy print function with — headers.
Definition: Timer.hpp:50
CLI::Timer::to_string
std::string to_string() const
This is the main function, it creates a string.
Definition: Timer.hpp:108
CLI::Timer
This is a simple timer with pretty printing. Creating the timer starts counting.
Definition: Timer.hpp:22