Libftpp
A modern C++ library
chronometre.cpp
Go to the documentation of this file.
1 #include "chronometre.hpp"
2 
4 {
5  _timestamps.clear();
6 }
7 
8 void Chronometre::createTimestamp()
9 {
10  _now = std::chrono::system_clock::now();
11  _timestamps.push_back(
12  std::chrono::duration_cast<std::chrono::nanoseconds>(_now.time_since_epoch()).count());
13 }
18 {
19  createTimestamp();
20 }
21 
26 {
27  if (_timestamps.size() == 0)
28  throw std::logic_error("Chronometre::end(): start() must be called before end()");
29 
30  createTimestamp();
31 }
32 
37 {
38  if (_timestamps.size() > 1)
39  {
40  _timestamps.pop_back();
41  _timestamps.pop_back();
42  }
43 }
44 
50 {
51  if (_timestamps.size() == 0 || _timestamps.size() % 2 != 0)
52  throw std::logic_error("Chronometre::getTime(): start() and end() must be called in pairs");
53 
54  return _timestamps.back() - _timestamps[_timestamps.size() - 2];
55 }
56 
62 {
63  if (_timestamps.size() == 0 || _timestamps.size() % 2 != 0)
64  throw std::logic_error("Chronometre::getTime(): start() and end() must be called in pairs");
65 
66  return (_timestamps.back() - _timestamps[_timestamps.size() - 2]) / 1000.0;
67 }
68 
74 {
75  if (_timestamps.size() == 0 || _timestamps.size() % 2 != 0)
76  throw std::logic_error("Chronometre::getTime(): start() and end() must be called in pairs");
77 
78  return (_timestamps.back() - _timestamps[_timestamps.size() - 2]) / 1000000.0;
79 }
80 
86 {
87  if (_timestamps.size() == 0 || _timestamps.size() % 2 != 0)
88  throw std::logic_error("Chronometre::getTime(): start() and end() must be called in pairs");
89 
90  return (_timestamps.back() - _timestamps[_timestamps.size() - 2]) / 1000000000.0;
91 }
92 
98 std::string Chronometre::getTimeString() const
99 {
100  if (_timestamps.size() == 0 || _timestamps.size() % 2 != 0)
101  throw std::logic_error("Chronometre::getTime(): start() and end() must be called in pairs");
102 
103  double nanoseconds = _timestamps.back() - _timestamps[_timestamps.size() - 2];
104  if (nanoseconds < 1000)
105  return std::to_string(nanoseconds) + " ns";
106  else if (nanoseconds < 1000000)
107  return std::to_string(nanoseconds / 1000.0) + " µs";
108  else if (nanoseconds < 1000000000)
109  return std::to_string(nanoseconds / 1000000.0) + " ms";
110  else
111  return std::to_string(nanoseconds / 1000000000.0) + " s";
112 }
void start()
Start the chronometre.
Definition: chronometre.cpp:17
void popLastChrono()
Remove the last recorded time interval (start and end timestamps).
Definition: chronometre.cpp:36
std::string getTimeString() const
Get the elapsed time as a formatted string with appropriate units.
Definition: chronometre.cpp:98
double getTimeMicroseconds() const
Get the elapsed time in microseconds between the last start and end timestamps.
Definition: chronometre.cpp:61
double getTimeSeconds() const
Get the elapsed time in seconds between the last start and end timestamps.
Definition: chronometre.cpp:85
double getTimeMilliseconds() const
Get the elapsed time in milliseconds between the last start and end timestamps.
Definition: chronometre.cpp:73
double getTimeNanoseconds() const
Get the elapsed time in nanoseconds between the last start and end timestamps.
Definition: chronometre.cpp:49
void end()
End the chronometre.
Definition: chronometre.cpp:25