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 {
public:
Timer() { clock_gettime(CLOCK_REALTIME, &beg_); }
Timer() { clock_gettime(CLOCK_MONOTONIC, &beg_); }
double elapsed() {
clock_gettime(CLOCK_REALTIME, &end_);
clock_gettime(CLOCK_MONOTONIC, &end_);
return end_.tv_sec - beg_.tv_sec +
(end_.tv_nsec - beg_.tv_nsec) / 1000000000.;
}
void reset() { clock_gettime(CLOCK_REALTIME, &beg_); }
void reset() { clock_gettime(CLOCK_MONOTONIC, &beg_); }
private:
timespec beg_, end_;