jak-project/game/system/Timer.h

29 lines
605 B
C
Raw Normal View History

2020-08-22 22:30:12 -04:00
#ifndef RUNTIME_TIMER_H
#define RUNTIME_TIMER_H
#include <cstdint>
#include <ctime>
#include <cstdint>
class Timer {
2020-08-26 01:21:33 -04:00
public:
explicit Timer() { start(); }
2020-08-22 22:30:12 -04:00
2020-08-26 01:21:33 -04:00
void start() { clock_gettime(CLOCK_MONOTONIC, &_startTime); }
2020-08-22 22:30:12 -04:00
2020-08-26 01:21:33 -04:00
double getMs() { return (double)getNs() / 1.e6; }
2020-08-22 22:30:12 -04:00
int64_t getNs() {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
2020-08-26 01:21:33 -04:00
return (int64_t)(now.tv_nsec - _startTime.tv_nsec) +
1000000000 * (now.tv_sec - _startTime.tv_sec);
2020-08-22 22:30:12 -04:00
}
2020-08-26 01:21:33 -04:00
double getSeconds() { return (double)getNs() / 1.e9; }
2020-08-22 22:30:12 -04:00
struct timespec _startTime;
};
2020-08-26 01:21:33 -04:00
#endif // RUNTIME_TIMER_H