5#include <sys/resource.h>
14 struct timespec _startTime;
15 struct timespec _endTime;
17 static constexpr int64_t NanosecondsPerSecond = 1000LL * 1000 * 1000;
22 void start() { clock_gettime(CLOCK_REALTIME, &_startTime); }
27 void end() { clock_gettime(CLOCK_REALTIME, &_endTime); }
46 if (_endTime.tv_nsec == 0 && _endTime.tv_sec == 0) {
47 clock_gettime(CLOCK_REALTIME, &end);
52 int64_t nanos = (end.tv_sec - _startTime.tv_sec) * NanosecondsPerSecond;
53 nanos += (end.tv_nsec - _startTime.tv_nsec);
64 return nanos / 1000000;
73 double secs =
static_cast<double>(nanos) / NanosecondsPerSecond;
83 return std::to_string(secs);
Get the execution between a block of code.
Definition: timer.hpp:12
int64_t nanoseconds() const
Return the number of nanoseconds elapsed since the start of the timer.
Definition: timer.hpp:43
Timer()
Initialize a Timer with the current time.
Definition: timer.hpp:34
double seconds() const
Return the number of seconds elapsed since the start of the timer.
Definition: timer.hpp:70
std::string toString() const
Return the number of seconds elapsed since the start of the timer as a string.
Definition: timer.hpp:80
int64_t milliseconds() const
Return the number of nanoseconds elapsed since the start of the timer.
Definition: timer.hpp:61