1#ifndef XPED_STOPWATCH_HPP_
2#define XPED_STOPWATCH_HPP_
10#include "fmt/chrono.h"
25std::string
format_secs(std::chrono::duration<
double, std::ratio<1, 1>> dts)
27 if(dts.count() < 60.) {
28 return fmt::format(
"{:.2}", dts);
29 }
else if(dts.count() >= 60. && dts.count() < 3600.) {
30 std::chrono::duration<double, std::ratio<60, 1>> dtm = dts;
31 return fmt::format(
"{:.2}", dtm);
32 }
else if(dts.count() >= 3600. && dts.count() < 86400.) {
33 std::chrono::duration<double, std::ratio<3600, 1>> dth = dts;
34 return fmt::format(
"{:.2}", dth);
36 std::chrono::duration<double, std::ratio<86400, 1>> dtd = dts;
37 return fmt::format(
"{:.2}d", dtd.count());
40template <
typename ClockClass = std::chrono::high_resolution_clock>
48 std::chrono::seconds
time();
53 template <
typename ThemeType>
54 std::string
info(ThemeType theme,
bool RESTART =
true);
55 inline std::string
info();
57 template <
typename ThemeType>
58 void check(ThemeType theme);
61 template <
typename ThemeType,
class F,
class... ArgTypes>
62 std::invoke_result_t<F && (ArgTypes && ...)>
runTime(ThemeType theme, F&& f, ArgTypes&&... args);
65 std::chrono::time_point<ClockClass> t_start, t_end;
70template <
typename ClockClass>
73 SAVING_TO_FILE =
false;
77template <
typename ClockClass>
80 filename = filename_input;
81 std::ofstream file(filename, std::ios::trunc);
83 outfile.open(filename, std::fstream::app | std::fstream::out);
85 SAVING_TO_FILE =
true;
89template <
typename ClockClass>
92 t_end = ClockClass::now();
93 std::chrono::duration<double, std::ratio<1, 1>> dt = t_end - t_start;
95 std::chrono::duration<double, std::ratio<1, 1000>> dtms = dt;
96 return fmt::format(
"{}", dtms);
98 return fmt::format(
"{}", dt);
100 std::chrono::duration<double, std::ratio<60, 1>> dtm = dt;
101 return fmt::format(
"{}", dtm);
103 std::chrono::duration<double, std::ratio<3600, 1>> dth = dt;
104 return fmt::format(
"{}", dth);
106 std::chrono::duration<double, std::ratio<86400, 1>> dtd = dt;
107 return fmt::format(
"{}d", dtd.count());
112template <
typename ClockClass>
115 t_end = ClockClass::now();
116 return std::chrono::duration_cast<std::chrono::seconds>(t_end - t_start);
119template <
typename ClockClass>
122 t_end = ClockClass::now();
123 return std::chrono::duration<double, std::ratio<1, 1>>(t_end - t_start).count();
126template <
typename ClockClass>
129 t_start = ClockClass::now();
132template <
typename ClockClass>
133template <
typename ThemeType>
136 std::cout << info(theme) << std::endl;
139template <
typename ClockClass>
140template <
typename ThemeType>
143 t_end = ClockClass::now();
145 std::chrono::duration<double, std::ratio<1, 1>> dtTest = t_end - t_start;
147 std::stringstream ss;
148 if(dtTest.count() < 60.) {
149 std::chrono::duration<double, std::ratio<1, 1>> dt = t_end - t_start;
150 ss << theme <<
": " << dt.count() <<
" #s";
151 }
else if(dtTest.count() >= 60. && dtTest.count() < 3600.) {
152 std::chrono::duration<double, std::ratio<60, 1>> dt = t_end - t_start;
153 ss << theme <<
": " << dt.count() <<
" #min";
154 }
else if(dtTest.count() >= 3600. && dtTest.count() < 86400.) {
155 std::chrono::duration<double, std::ratio<3600, 1>> dt = t_end - t_start;
156 ss << theme <<
": " << dt.count() <<
" #h";
158 std::chrono::duration<double, std::ratio<86400, 1>> dt = t_end - t_start;
159 ss << theme <<
": " << dt.count() <<
" #d";
162 if(SAVING_TO_FILE ==
true) {
163 std::fstream outfile;
164 outfile.open(filename, std::fstream::app | std::fstream::out);
165 outfile << ss.str() << std::endl;
169 if(RESTART) { start(); }
173template <
typename ClockClass>
174template <
typename ThemeType,
class F,
class... ArgTypes>
178 if constexpr(std::is_void<std::invoke_result_t<F && (ArgTypes && ...)>>::value) {
179 return std::invoke(std::forward<F>(f), std::forward<ArgTypes>(args)...);
183 std::invoke_result_t<F && (ArgTypes && ...)> result = std::invoke(std::forward<F>(f), std::forward<ArgTypes>(args)...);
184 if(SAVING_TO_FILE ==
false) {
185 std::cout << info(theme) << std::endl;
193template <
typename ClockClass>
199template <
typename ClockClass>
Definition: Stopwatch.hpp:42
double seconds()
Definition: Stopwatch.hpp:120
std::string time_string(TimeUnit u=TimeUnit::NATURAL)
Definition: Stopwatch.hpp:90
void check()
Definition: Stopwatch.hpp:194
void start()
Definition: Stopwatch.hpp:127
Stopwatch()
Definition: Stopwatch.hpp:71
std::chrono::seconds time()
Definition: Stopwatch.hpp:113
std::invoke_result_t< F &&(ArgTypes &&...)> runTime(ThemeType theme, F &&f, ArgTypes &&... args)
Definition: Stopwatch.hpp:175
std::string info()
Definition: Stopwatch.hpp:200
Definition: FusionTree.hpp:15
TimeUnit
Definition: Stopwatch.hpp:16
std::string format_secs(std::chrono::duration< double, std::ratio< 1, 1 > > dts)
Definition: Stopwatch.hpp:25