Loading...
Searching...
No Matches
Timer.hpp
Go to the documentation of this file.
// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
// IWYU pragma: private, include "CLI/CLI.hpp"
// On GCC < 4.8, the following define is often missing. Due to the
// fact that this library only uses sleep_for, this should be safe
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5 && __GNUC_MINOR__ < 8
#define _GLIBCXX_USE_NANOSLEEP
#endif
#include <cmath>
#include <array>
#include <chrono>
#include <cstdio>
#include <functional>
#include <iostream>
#include <string>
#include <utility>
namespace CLI {
class Timer {
protected:
using clock = std::chrono::steady_clock;
using time_point = std::chrono::time_point<clock>;
using time_print_t = std::function<std::string(std::string, std::string)>;
std::string title_;
time_print_t time_print_;
time_point start_;
std::size_t cycles{1};
public:
static std::string Simple(std::string title, std::string time) { return title + ": " + time; }
static std::string Big(std::string title, std::string time) {
return std::string("-----------------------------------------\n") + "| " + title + " | Time = " + time + "\n" +
"-----------------------------------------";
}
public:
explicit Timer(std::string title = "Timer", time_print_t time_print = Simple)
: title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {}
std::string time_it(std::function<void()> f, double target_time = 1) {
time_point start = start_;
double total_time = NAN;
start_ = clock::now();
std::size_t n = 0;
do {
f();
std::chrono::duration<double> elapsed = clock::now() - start_;
total_time = elapsed.count();
} while(n++ < 100u && total_time < target_time);
std::string out = make_time_str(total_time / static_cast<double>(n)) + " for " + std::to_string(n) + " tries";
start_ = start;
return out;
}
std::string make_time_str() const { // NOLINT(modernize-use-nodiscard)
time_point stop = clock::now();
std::chrono::duration<double> elapsed = stop - start_;
double time = elapsed.count() / static_cast<double>(cycles);
return make_time_str(time);
}
// LCOV_EXCL_START
std::string make_time_str(double time) const { // NOLINT(modernize-use-nodiscard)
auto print_it = [](double x, std::string unit) {
const unsigned int buffer_length = 50;
std::array<char, buffer_length> buffer;
std::snprintf(buffer.data(), buffer_length, "%.5g", x);
return buffer.data() + std::string(" ") + unit;
};
if(time < .000001)
return print_it(time * 1000000000, "ns");
if(time < .001)
return print_it(time * 1000000, "us");
if(time < 1)
return print_it(time * 1000, "ms");
return print_it(time, "s");
}
// LCOV_EXCL_STOP
std::string to_string() const { return time_print_(title_, make_time_str()); } // NOLINT(modernize-use-nodiscard)
Timer &operator/(std::size_t val) {
cycles = val;
return *this;
}
};
class AutoTimer : public Timer {
public:
explicit AutoTimer(std::string title = "Timer", time_print_t time_print = Simple) : Timer(title, time_print) {}
// GCC 4.7 does not support using inheriting constructors.
~AutoTimer() { std::cout << to_string() << '\n'; }
};
} // namespace CLI
inline std::ostream &operator<<(std::ostream &in, const CLI::Timer &timer) { return in << timer.to_string(); }
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:138
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:128
CLI::AutoTimer::~AutoTimer
~AutoTimer()
This destructor prints the string.
Definition Timer.hpp:132
CLI::Timer
This is a simple timer with pretty printing. Creating the timer starts counting.
Definition Timer.hpp:30
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:45
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:65
CLI::Timer::start_
time_point start_
This is the starting point (when the timer was created).
Definition Timer.hpp:48
CLI::Timer::title_
std::string title_
This is the title of the timer.
Definition Timer.hpp:42
CLI::Timer::make_time_str
std::string make_time_str() const
This formats the numerical value for the time string.
Definition Timer.hpp:87
CLI::Timer::to_string
std::string to_string() const
This is the main function, it creates a string.
Definition Timer.hpp:115
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:39
CLI::Timer::cycles
std::size_t cycles
This is the number of times cycles (print divides by this number).
Definition Timer.hpp:51
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:96
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:69
CLI::Timer::clock
std::chrono::steady_clock clock
This is a typedef to make clocks easier to use.
Definition Timer.hpp:33
CLI::Timer::operator/
Timer & operator/(std::size_t val)
Division sets the number of cycles to divide by (no graphical change).
Definition Timer.hpp:118
CLI::Timer::Big
static std::string Big(std::string title, std::string time)
This is a fancy print function with — headers.
Definition Timer.hpp:58
CLI::Timer::time_point
std::chrono::time_point< clock > time_point
This typedef is for points in time.
Definition Timer.hpp:36
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:55
CLI
Definition App.hpp:36