11#include <fmt/format.h>
13#include <boost/config.hpp>
17#if defined(_WIN32) || defined(WIN32)
26#if defined(_WIN32) || defined(WIN32)
28std::int_least64_t tick_factor()
31 static std::int_least64_t tick_factor = 0;
32 if (tick_factor == 0) {
33 if (tick_factor = ::sysconf(_SC_CLK_TCK); tick_factor <= 0) {
36 tick_factor = INT64_C(1000000000) / tick_factor;
37 if (tick_factor == 0) {
47 const std::chrono::nanoseconds x(
48 std::chrono::high_resolution_clock::now().time_since_epoch());
49 current->
wall = std::chrono::nanoseconds(x.count());
51#if defined(_WIN32) || defined(WIN32)
53 FILETIME creation, exit;
54 if (::GetProcessTimes(::GetCurrentProcess(), &creation, &exit,
55 (LPFILETIME)¤t->
system,
56 (LPFILETIME)¤t->
user)) {
62 current->
system = current->
user = std::chrono::nanoseconds(-1);
66 const clock_t c = ::times(&tm);
67 if (c ==
static_cast<clock_t
>(-1))
69 current->
system = current->
user = std::chrono::nanoseconds(-1);
71 current->
system = std::chrono::nanoseconds(tm.tms_stime + tm.tms_cstime);
72 current->
user = std::chrono::nanoseconds(tm.tms_utime + tm.tms_cutime);
74 if (factor = tick_factor(); factor != -1) {
75 current->
user *= factor;
78 current->
user = current->
system = std::chrono::nanoseconds(-1);
92 get_cpu_times(¤t);
101 const double sec = 1000000000.0L;
102 const double wall_sec =
static_cast<double>(
times_.
wall.count()) / sec;
104 const double total_sec =
static_cast<double>(total) / sec;
105 const double percent = (total_sec / wall_sec) * 100.0;
108 fmt::runtime(format), fmt::arg(
"w", wall_sec),
109 fmt::arg(
"u",
static_cast<double>(
times_.
user.count()) / sec),
110 fmt::arg(
"s",
static_cast<double>(
times_.
system.count()) / sec),
111 fmt::arg(
"t", total_sec), fmt::arg(
"p", percent));
126 get_cpu_times(¤t);
143 : format_(std::move(format)), output_(&std::cout) {}
146 : format_(std::move(format)), output_(&stream) {}
149 spdlog::level::level_enum level, std::string format)
150 : format_(std::move(format)),
151 output_(std::make_pair(std::move(logger), level)) {}
166 return *std::get<std::ostream*>(
output_);
170 return std::get<std::pair<std::shared_ptr<spdlog::logger>,
171 spdlog::level::level_enum>>(
output_)
180 (**ostream) <<
string <<
'\n';
182 std::get<1>(
output_).first->log(std::get<1>(
output_).second,
string);
std::variant< std::ostream *, std::pair< std::shared_ptr< spdlog::logger >, spdlog::level::level_enum > > output_
std::string Format(std::string_view format=Timer::kDefaultFormat) const
Return the number of elapsed seconds of the wall clock time, user time and system time as a formatted...
Timer::cpu_times Elapsed() const noexcept
Retrieve the elapsed time since the construction.
AutoTimer(std::string format=Timer::kDefaultFormat)
Create a new AutoTimer that will report time measurements to std::cout when destructed.
const std::shared_ptr< spdlog::logger > & logger() const
Retrieve the logger that was passed in the constructor.
~AutoTimer()
Destructor of AutoTimer, calls Report().
const std::string & FormatString() const
Retrieve the format string that was passed in the constructor.
std::ostream & ostream() const
Retrieve the output stream object that was passed in the constructor.
void Report()
Report time (wall, user, system) since construction of AutoTimer() to either a std::ostream or spdlog...
std::string Format(std::string_view format=kDefaultFormat) const
Return the number of elapsed seconds of the wall clock time, user time and system time as a formatted...
void Stop() noexcept
stop the timer, stop counting the elapsed time.
void Resume() noexcept
Resume the timer, i.e. start counting from where we stopped the last time.
cpu_times Elapsed() const noexcept
Elapsed time since timer start, doesn't stop the timer.
bool IsStopped() const noexcept
Is the timer currently stopped?
void Start() noexcept
(Re)starts the timer, i.e. the timer starts counting from 0 when this method is called.
Contains basic functionality that is used by other parts of LehrFEM++.
Packages the elapsed wall clock time, user process CPU time, and system process CPU time.
std::chrono::nanoseconds system
Elapsed System time in nano-seconds.
std::chrono::nanoseconds user
Elapsed User time in nano-seconds.
std::chrono::nanoseconds wall
Elapsed WallClock time in nano-seconds.