Feellgood
chronometer.h
1 #ifndef CHRONOMETER_H
2 #define CHRONOMETER_H
3 
4 #include<chrono>
5 #include <string>
6 #include <iomanip>
7 #include <sstream>
8 
9 std::string date(void);
10 
18  {
19  public:
21  chronometer():nb(0) { reset(); }
22 
24  chronometer(const int _nb):nb(_nb) { reset(); }
25 
27  double fp_elapsed()
28  {
29  start = end;
30  end = std::chrono::high_resolution_clock::now();
31  std::chrono::duration<double,std::ratio<1>> d = end-start;
32  return d.count();
33  }
34 
36  std::string convertSeconds(double d)
37  {
38  std::stringstream ss;
39  ss << d << " s";
40  if (d >= 3600)
41  ss << " (" << d / 3600 << " h)";
42  else if (d >= 60)
43  ss << " (" << d / 60 << " min)";
44  ss << '\n';
45  return ss.str();
46  }
47 
49  std::string millis() { return measure<std::milli>(" ms"); }
50 
52  std::string micros() { return measure<std::micro>(" µs"); }
53 
55  void reset(void)
56  { end = start = std::chrono::high_resolution_clock::now(); };
57  private:
59  const int nb;
60 
62  std::chrono::time_point<std::chrono::high_resolution_clock> start;
63 
65  std::chrono::time_point<std::chrono::high_resolution_clock> end;
66 
70  template<typename T>
71  std::string measure(const std::string str)
72  {
73  start = end;
74  end = std::chrono::high_resolution_clock::now();
75 
76  std::chrono::duration<double,T> d = end-start;
77 
78  std::stringstream ss;
79  ss << std::fixed << std::setprecision(nb) << d.count() << str;
80  return ss.str();
81  }
82  };
83 
84 #endif
Definition: chronometer.h:18
void reset(void)
Definition: chronometer.h:55
std::string millis()
Definition: chronometer.h:49
std::string convertSeconds(double d)
Definition: chronometer.h:36
chronometer(const int _nb)
Definition: chronometer.h:24
double fp_elapsed()
Definition: chronometer.h:27
const int nb
Definition: chronometer.h:56
std::chrono::time_point< std::chrono::high_resolution_clock > end
Definition: chronometer.h:65
chronometer()
Definition: chronometer.h:21
std::string measure(const std::string str)
Definition: chronometer.h:71
std::string micros()
Definition: chronometer.h:52
std::chrono::time_point< std::chrono::high_resolution_clock > start
Definition: chronometer.h:62