File indexing completed on 2025-07-05 08:51:43
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009
0010
0011
0012
0013 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5 && __GNUC_MINOR__ < 8
0014 #define _GLIBCXX_USE_NANOSLEEP
0015 #endif
0016
0017 #include <cmath>
0018
0019 #include <array>
0020 #include <chrono>
0021 #include <functional>
0022 #include <iostream>
0023 #include <string>
0024 #include <utility>
0025
0026 namespace CLI {
0027
0028
0029 class Timer {
0030 protected:
0031
0032 using clock = std::chrono::steady_clock;
0033
0034
0035 using time_point = std::chrono::time_point<clock>;
0036
0037
0038 using time_print_t = std::function<std::string(std::string, std::string)>;
0039
0040
0041 std::string title_;
0042
0043
0044 time_print_t time_print_;
0045
0046
0047 time_point start_;
0048
0049
0050 std::size_t cycles{1};
0051
0052 public:
0053
0054 static std::string Simple(std::string title, std::string time) { return title + ": " + time; }
0055
0056
0057 static std::string Big(std::string title, std::string time) {
0058 return std::string("-----------------------------------------\n") + "| " + title + " | Time = " + time + "\n" +
0059 "-----------------------------------------";
0060 }
0061
0062 public:
0063
0064 explicit Timer(std::string title = "Timer", time_print_t time_print = Simple)
0065 : title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {}
0066
0067
0068 std::string time_it(std::function<void()> f, double target_time = 1) {
0069 time_point start = start_;
0070 double total_time = NAN;
0071
0072 start_ = clock::now();
0073 std::size_t n = 0;
0074 do {
0075 f();
0076 std::chrono::duration<double> elapsed = clock::now() - start_;
0077 total_time = elapsed.count();
0078 } while(n++ < 100u && total_time < target_time);
0079
0080 std::string out = make_time_str(total_time / static_cast<double>(n)) + " for " + std::to_string(n) + " tries";
0081 start_ = start;
0082 return out;
0083 }
0084
0085
0086 std::string make_time_str() const {
0087 time_point stop = clock::now();
0088 std::chrono::duration<double> elapsed = stop - start_;
0089 double time = elapsed.count() / static_cast<double>(cycles);
0090 return make_time_str(time);
0091 }
0092
0093
0094
0095 std::string make_time_str(double time) const {
0096 auto print_it = [](double x, std::string unit) {
0097 const unsigned int buffer_length = 50;
0098 std::array<char, buffer_length> buffer;
0099 std::snprintf(buffer.data(), buffer_length, "%.5g", x);
0100 return buffer.data() + std::string(" ") + unit;
0101 };
0102
0103 if(time < .000001)
0104 return print_it(time * 1000000000, "ns");
0105 if(time < .001)
0106 return print_it(time * 1000000, "us");
0107 if(time < 1)
0108 return print_it(time * 1000, "ms");
0109 return print_it(time, "s");
0110 }
0111
0112
0113
0114 std::string to_string() const { return time_print_(title_, make_time_str()); }
0115
0116
0117 Timer &operator/(std::size_t val) {
0118 cycles = val;
0119 return *this;
0120 }
0121 };
0122
0123
0124 class AutoTimer : public Timer {
0125 public:
0126
0127 explicit AutoTimer(std::string title = "Timer", time_print_t time_print = Simple) : Timer(title, time_print) {}
0128
0129
0130
0131 ~AutoTimer() { std::cout << to_string() << '\n'; }
0132 };
0133
0134 }
0135
0136
0137 inline std::ostream &operator<<(std::ostream &in, const CLI::Timer &timer) { return in << timer.to_string(); }