8 void Chronometre::createTimestamp()
10 _now = std::chrono::system_clock::now();
11 _timestamps.push_back(
12 std::chrono::duration_cast<std::chrono::nanoseconds>(_now.time_since_epoch()).count());
27 if (_timestamps.size() == 0)
28 throw std::logic_error(
"Chronometre::end(): start() must be called before end()");
38 if (_timestamps.size() > 1)
40 _timestamps.pop_back();
41 _timestamps.pop_back();
51 if (_timestamps.size() == 0 || _timestamps.size() % 2 != 0)
52 throw std::logic_error(
"Chronometre::getTime(): start() and end() must be called in pairs");
54 return _timestamps.back() - _timestamps[_timestamps.size() - 2];
63 if (_timestamps.size() == 0 || _timestamps.size() % 2 != 0)
64 throw std::logic_error(
"Chronometre::getTime(): start() and end() must be called in pairs");
66 return (_timestamps.back() - _timestamps[_timestamps.size() - 2]) / 1000.0;
75 if (_timestamps.size() == 0 || _timestamps.size() % 2 != 0)
76 throw std::logic_error(
"Chronometre::getTime(): start() and end() must be called in pairs");
78 return (_timestamps.back() - _timestamps[_timestamps.size() - 2]) / 1000000.0;
87 if (_timestamps.size() == 0 || _timestamps.size() % 2 != 0)
88 throw std::logic_error(
"Chronometre::getTime(): start() and end() must be called in pairs");
90 return (_timestamps.back() - _timestamps[_timestamps.size() - 2]) / 1000000000.0;
100 if (_timestamps.size() == 0 || _timestamps.size() % 2 != 0)
101 throw std::logic_error(
"Chronometre::getTime(): start() and end() must be called in pairs");
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";
111 return std::to_string(nanoseconds / 1000000000.0) +
" s";
void start()
Start the chronometre.
void popLastChrono()
Remove the last recorded time interval (start and end timestamps).
std::string getTimeString() const
Get the elapsed time as a formatted string with appropriate units.
double getTimeMicroseconds() const
Get the elapsed time in microseconds between the last start and end timestamps.
double getTimeSeconds() const
Get the elapsed time in seconds between the last start and end timestamps.
double getTimeMilliseconds() const
Get the elapsed time in milliseconds between the last start and end timestamps.
double getTimeNanoseconds() const
Get the elapsed time in nanoseconds between the last start and end timestamps.
void end()
End the chronometre.