Xped
Loading...
Searching...
No Matches
Stopwatch.hpp
Go to the documentation of this file.
1#ifndef XPED_STOPWATCH_HPP_
2#define XPED_STOPWATCH_HPP_
3
4#include <chrono>
5#include <fstream>
6#include <functional>
7#include <iostream>
8#include <sstream>
9
10#include "fmt/chrono.h"
11#include "fmt/core.h"
12
13namespace Xped::util {
14
15enum class TimeUnit
16{
18 SECONDS,
19 MINUTES,
20 HOURS,
21 DAYS,
23};
24
25std::string format_secs(std::chrono::duration<double, std::ratio<1, 1>> dts)
26{
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);
35 }
36 std::chrono::duration<double, std::ratio<86400, 1>> dtd = dts;
37 return fmt::format("{:.2}d", dtd.count());
38}
39
40template <typename ClockClass = std::chrono::high_resolution_clock>
42{
43public:
44 Stopwatch();
45 Stopwatch(std::string filename_input);
46
47 std::string time_string(TimeUnit u = TimeUnit::NATURAL);
48 std::chrono::seconds time();
49 double seconds();
50
51 void start();
52
53 template <typename ThemeType>
54 std::string info(ThemeType theme, bool RESTART = true);
55 inline std::string info();
56
57 template <typename ThemeType>
58 void check(ThemeType theme);
59 void check();
60
61 template <typename ThemeType, class F, class... ArgTypes>
62 std::invoke_result_t<F && (ArgTypes && ...)> runTime(ThemeType theme, F&& f, ArgTypes&&... args);
63
64private:
65 std::chrono::time_point<ClockClass> t_start, t_end;
66 std::string filename;
67 bool SAVING_TO_FILE;
68};
69
70template <typename ClockClass>
72{
73 SAVING_TO_FILE = false;
74 start();
75}
76
77template <typename ClockClass>
78Stopwatch<ClockClass>::Stopwatch(std::string filename_input)
79{
80 filename = filename_input;
81 std::ofstream file(filename, std::ios::trunc); // delete file contents
82 std::fstream outfile;
83 outfile.open(filename, std::fstream::app | std::fstream::out);
84 outfile.close();
85 SAVING_TO_FILE = true;
86 start();
87}
88
89template <typename ClockClass>
91{
92 t_end = ClockClass::now();
93 std::chrono::duration<double, std::ratio<1, 1>> dt = t_end - t_start;
94 if(u == TimeUnit::MILLISECONDS) {
95 std::chrono::duration<double, std::ratio<1, 1000>> dtms = dt;
96 return fmt::format("{}", dtms);
97 } else if(u == TimeUnit::SECONDS) {
98 return fmt::format("{}", dt);
99 } else if(u == TimeUnit::MINUTES) {
100 std::chrono::duration<double, std::ratio<60, 1>> dtm = dt;
101 return fmt::format("{}", dtm);
102 } else if(u == TimeUnit::HOURS) {
103 std::chrono::duration<double, std::ratio<3600, 1>> dth = dt;
104 return fmt::format("{}", dth);
105 } else if(u == TimeUnit::DAYS) {
106 std::chrono::duration<double, std::ratio<86400, 1>> dtd = dt;
107 return fmt::format("{}d", dtd.count());
108 }
109 return format_secs(dt);
110}
111
112template <typename ClockClass>
113std::chrono::seconds Stopwatch<ClockClass>::time()
114{
115 t_end = ClockClass::now();
116 return std::chrono::duration_cast<std::chrono::seconds>(t_end - t_start);
117}
118
119template <typename ClockClass>
121{
122 t_end = ClockClass::now();
123 return std::chrono::duration<double, std::ratio<1, 1>>(t_end - t_start).count();
124}
125
126template <typename ClockClass>
128{
129 t_start = ClockClass::now();
130}
131
132template <typename ClockClass>
133template <typename ThemeType>
134void Stopwatch<ClockClass>::check(ThemeType theme)
135{
136 std::cout << info(theme) << std::endl;
137}
138
139template <typename ClockClass>
140template <typename ThemeType>
141std::string Stopwatch<ClockClass>::info(ThemeType theme, bool RESTART)
142{
143 t_end = ClockClass::now();
144
145 std::chrono::duration<double, std::ratio<1, 1>> dtTest = t_end - t_start;
146
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";
157 } else {
158 std::chrono::duration<double, std::ratio<86400, 1>> dt = t_end - t_start;
159 ss << theme << ": " << dt.count() << " #d";
160 }
161
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;
166 outfile.close();
167 }
168
169 if(RESTART) { start(); }
170 return ss.str();
171}
172
173template <typename ClockClass>
174template <typename ThemeType, class F, class... ArgTypes>
175std::invoke_result_t<F && (ArgTypes && ...)> Stopwatch<ClockClass>::runTime(ThemeType theme, F&& f, ArgTypes&&... args)
176{
177 start();
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)...);
180 // if (SAVING_TO_FILE == false) { std::cout << info(theme) << std::endl; }
181 // else { info(theme); }
182 } else {
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;
186 } else {
187 info(theme);
188 }
189 return result;
190 }
191}
192
193template <typename ClockClass>
195{
196 check("");
197}
198
199template <typename ClockClass>
200inline std::string Stopwatch<ClockClass>::info()
201{
202 return info("");
203}
204
205} // namespace Xped::util
206#endif
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