Files
jak-project/old_compiler/cpp/goal/Timer.h
T
2020-08-27 11:58:19 -04:00

52 lines
996 B
C++

/*! @file Timer.h
* @brief Timer for measuring how long things take
*/
#ifndef PROJECT_TIMER_H
#define PROJECT_TIMER_H
#include <assert.h>
#include <stdint.h>
#include <time.h>
/*!
* Timer for measuring time elapsed with clock_monotonic
*/
class Timer {
public:
/*!
* Construct and start timer
*/
explicit Timer() { start(); }
/*!
* Start the timer
*/
void start() { clock_gettime(CLOCK_MONOTONIC, &_startTime); }
/*!
* Get milliseconds elapsed
*/
double getMs() { return (double)getNs() / 1.e6; }
double getUs() { return (double)getNs() / 1.e3; }
/*!
* Get nanoseconds elapsed
*/
int64_t getNs() {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (int64_t)(now.tv_nsec - _startTime.tv_nsec) +
1000000000 * (now.tv_sec - _startTime.tv_sec);
}
/*!
* Get seconds elapsed
*/
double getSeconds() { return (double)getNs() / 1.e9; }
struct timespec _startTime;
};
#endif // PROJECT_TIMER_H