CLI11  2.1.2
Timer.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2021, University of Cincinnati, developed by Henry Schreiner
2 // under NSF AWARD 1414736 and by the respective contributors.
3 // All rights reserved.
4 //
5 // SPDX-License-Identifier: BSD-3-Clause
6 
7 #pragma once
8 
9 // On GCC < 4.8, the following define is often missing. Due to the
10 // fact that this library only uses sleep_for, this should be safe
11 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5 && __GNUC_MINOR__ < 8
12 #define _GLIBCXX_USE_NANOSLEEP
13 #endif
14 
15 #include <array>
16 #include <chrono>
17 #include <functional>
18 #include <iostream>
19 #include <string>
20 #include <utility>
21 
22 namespace CLI {
23 
25 class Timer {
26  protected:
28  using clock = std::chrono::steady_clock;
29 
31  using time_point = std::chrono::time_point<clock>;
32 
34  using time_print_t = std::function<std::string(std::string, std::string)>;
35 
37  std::string title_;
38 
41 
44 
46  std::size_t cycles{1};
47 
48  public:
50  static std::string Simple(std::string title, std::string time) { return title + ": " + time; }
51 
53  static std::string Big(std::string title, std::string time) {
54  return std::string("-----------------------------------------\n") + "| " + title + " | Time = " + time + "\n" +
55  "-----------------------------------------";
56  }
57 
58  public:
60  explicit Timer(std::string title = "Timer", time_print_t time_print = Simple)
61  : title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {}
62 
64  std::string time_it(std::function<void()> f, double target_time = 1) {
65  time_point start = start_;
66  double total_time;
67 
68  start_ = clock::now();
69  std::size_t n = 0;
70  do {
71  f();
72  std::chrono::duration<double> elapsed = clock::now() - start_;
73  total_time = elapsed.count();
74  } while(n++ < 100u && total_time < target_time);
75 
76  std::string out = make_time_str(total_time / static_cast<double>(n)) + " for " + std::to_string(n) + " tries";
77  start_ = start;
78  return out;
79  }
80 
82  std::string make_time_str() const {
83  time_point stop = clock::now();
84  std::chrono::duration<double> elapsed = stop - start_;
85  double time = elapsed.count() / static_cast<double>(cycles);
86  return make_time_str(time);
87  }
88 
89  // LCOV_EXCL_START
91  std::string make_time_str(double time) const {
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;
97  };
98 
99  if(time < .000001)
100  return print_it(time * 1000000000, "ns");
101  else if(time < .001)
102  return print_it(time * 1000000, "us");
103  else if(time < 1)
104  return print_it(time * 1000, "ms");
105  else
106  return print_it(time, "s");
107  }
108  // LCOV_EXCL_STOP
109 
111  std::string to_string() const { return time_print_(title_, make_time_str()); }
112 
114  Timer &operator/(std::size_t val) {
115  cycles = val;
116  return *this;
117  }
118 };
119 
121 class AutoTimer : public Timer {
122  public:
124  explicit AutoTimer(std::string title = "Timer", time_print_t time_print = Simple) : Timer(title, time_print) {}
125  // GCC 4.7 does not support using inheriting constructors.
126 
128  ~AutoTimer() { std::cout << to_string() << std::endl; }
129 };
130 
131 } // namespace CLI
132 
134 inline std::ostream &operator<<(std::ostream &in, const CLI::Timer &timer) { return in << timer.to_string(); }
Timer(std::string title="Timer", time_print_t time_print=Simple)
Standard constructor, can set title and print function.
Definition: Timer.hpp:60
Definition: App.hpp:34
Timer & operator/(std::size_t val)
Division sets the number of cycles to divide by (no graphical change)
Definition: Timer.hpp:114
std::string to_string() const
This is the main function, it creates a string.
Definition: Timer.hpp:111
STL namespace.
This class prints out the time upon destruction.
Definition: Timer.hpp:121
std::string make_time_str(double time) const
This prints out a time string from a time.
Definition: Timer.hpp:91
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:269
static std::string Big(std::string title, std::string time)
This is a fancy print function with — headers.
Definition: Timer.hpp:53
std::chrono::steady_clock clock
This is a typedef to make clocks easier to use.
Definition: Timer.hpp:28
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::string make_time_str() const
This formats the numerical value for the time string.
Definition: Timer.hpp:82
std::size_t cycles
This is the number of times cycles (print divides by this number)
Definition: Timer.hpp:46
~AutoTimer()
This destructor prints the string.
Definition: Timer.hpp:128
static std::string Simple(std::string title, std::string time)
Standard print function, this one is set by default.
Definition: Timer.hpp:50
std::string title_
This is the title of the timer.
Definition: Timer.hpp:37
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
time_point start_
This is the starting point (when the timer was created)
Definition: Timer.hpp:43
This is a simple timer with pretty printing. Creating the timer starts counting.
Definition: Timer.hpp:25
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
time_print_t time_print_
This is the function that is used to format most of the timing message.
Definition: Timer.hpp:40
std::chrono::time_point< clock > time_point
This typedef is for points in time.
Definition: Timer.hpp:31
AutoTimer(std::string title="Timer", time_print_t time_print=Simple)
Reimplementing the constructor is required in GCC 4.7.
Definition: Timer.hpp:124