Use steady/monotonic clock for the timers, so they can handle realtime clock jumps.

This commit is contained in:
Daniel K. O. (dkosmari) 2024-05-31 17:50:24 -03:00 committed by Maschell
parent 36993e1630
commit c8dbca8fad

View File

@ -3,15 +3,15 @@
class Timer { class Timer {
public: public:
Timer() { clock_gettime(CLOCK_REALTIME, &beg_); } Timer() { clock_gettime(CLOCK_MONOTONIC, &beg_); }
double elapsed() { double elapsed() {
clock_gettime(CLOCK_REALTIME, &end_); clock_gettime(CLOCK_MONOTONIC, &end_);
return end_.tv_sec - beg_.tv_sec + return end_.tv_sec - beg_.tv_sec +
(end_.tv_nsec - beg_.tv_nsec) / 1000000000.; (end_.tv_nsec - beg_.tv_nsec) / 1000000000.;
} }
void reset() { clock_gettime(CLOCK_REALTIME, &beg_); } void reset() { clock_gettime(CLOCK_MONOTONIC, &beg_); }
private: private:
timespec beg_, end_; timespec beg_, end_;