CLI11
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
Timer.hpp
1// Copyright (c) 2017-2026, 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// IWYU pragma: private, include "CLI/CLI.hpp"
10
11#include <cmath>
12
13#include <array>
14#include <chrono>
15#include <cstdio>
16#include <functional>
17#include <iostream>
18#include <string>
19#include <utility>
20
21namespace CLI {
22
24class Timer {
25 protected:
27 using clock = std::chrono::steady_clock;
28
30 using time_point = std::chrono::time_point<clock>;
31
33 using time_print_t = std::function<std::string(std::string, std::string)>;
34
36 std::string title_;
37
40
43
45 std::size_t cycles{1};
46
47 public:
49 static std::string Simple(std::string title, std::string time) { return title + ": " + time; }
50
52 static std::string Big(std::string title, std::string time) {
53 return std::string("-----------------------------------------\n") + "| " + title + " | Time = " + time + "\n" +
54 "-----------------------------------------";
55 }
56
57 public:
59 explicit Timer(std::string title = "Timer", time_print_t time_print = Simple)
60 : title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {}
61
63 std::string time_it(std::function<void()> f, double target_time = 1) {
64 time_point start = start_;
65 double total_time = NAN;
66
67 start_ = clock::now();
68 std::size_t n = 0;
69 do {
70 f();
71 std::chrono::duration<double> elapsed = clock::now() - start_;
72 total_time = elapsed.count();
73 } while(n++ < 100u && total_time < target_time);
74
75 std::string out = make_time_str(total_time / static_cast<double>(n)) + " for " + std::to_string(n) + " tries";
76 start_ = start;
77 return out;
78 }
79
81 std::string make_time_str() const { // NOLINT(modernize-use-nodiscard)
82 time_point stop = clock::now();
83 std::chrono::duration<double> elapsed = stop - start_;
84 double time = elapsed.count() / static_cast<double>(cycles);
85 return make_time_str(time);
86 }
87
88 // LCOV_EXCL_START
90 std::string make_time_str(double time) const { // NOLINT(modernize-use-nodiscard)
91 auto print_it = [](double x, std::string unit) {
92 const unsigned int buffer_length = 50;
93 std::array<char, buffer_length> buffer;
94 std::snprintf(buffer.data(), buffer_length, "%.5g", x);
95 return buffer.data() + std::string(" ") + unit;
96 };
97
98 if(time < .000001)
99 return print_it(time * 1000000000, "ns");
100 if(time < .001)
101 return print_it(time * 1000000, "us");
102 if(time < 1)
103 return print_it(time * 1000, "ms");
104 return print_it(time, "s");
105 }
106 // LCOV_EXCL_STOP
107
109 std::string to_string() const { return time_print_(title_, make_time_str()); } // NOLINT(modernize-use-nodiscard)
110
112 Timer &operator/(std::size_t val) {
113 cycles = val;
114 return *this;
115 }
116};
117
119class AutoTimer : public Timer {
120 public:
121 using Timer::Timer;
122
124 ~AutoTimer() { std::cout << to_string() << '\n'; }
125};
126
127} // namespace CLI
128
130inline 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:119
~AutoTimer()
This destructor prints the string.
Definition Timer.hpp:124
This is a simple timer with pretty printing. Creating the timer starts counting.
Definition Timer.hpp:24
time_print_t time_print_
This is the function that is used to format most of the timing message.
Definition Timer.hpp:39
Timer(std::string title="Timer", time_print_t time_print=Simple)
Standard constructor, can set title and print function.
Definition Timer.hpp:59
time_point start_
This is the starting point (when the timer was created)
Definition Timer.hpp:42
std::string title_
This is the title of the timer.
Definition Timer.hpp:36
std::string make_time_str() const
This formats the numerical value for the time string.
Definition Timer.hpp:81
std::string to_string() const
This is the main function, it creates a string.
Definition Timer.hpp:109
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:33
std::size_t cycles
This is the number of times cycles (print divides by this number)
Definition Timer.hpp:45
std::string make_time_str(double time) const
This prints out a time string from a time.
Definition Timer.hpp:90
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:63
std::chrono::steady_clock clock
This is a typedef to make clocks easier to use.
Definition Timer.hpp:27
Timer & operator/(std::size_t val)
Division sets the number of cycles to divide by (no graphical change)
Definition Timer.hpp:112
static std::string Big(std::string title, std::string time)
This is a fancy print function with — headers.
Definition Timer.hpp:52
std::chrono::time_point< clock > time_point
This typedef is for points in time.
Definition Timer.hpp:30
static std::string Simple(std::string title, std::string time)
Standard print function, this one is set by default.
Definition Timer.hpp:49