fxos/lib/util/Timer.cpp

67 lines
1.7 KiB
C++

//---------------------------------------------------------------------------//
// 1100101 |_ mov #0, r4 __ //
// 11 |_ <0xb380 %5c4> / _|_ _____ ___ //
// 0110 |_ 3.50 -> 3.60 | _\ \ / _ (_-< //
// |_ base# + offset |_| /_\_\___/__/ //
//---------------------------------------------------------------------------//
#include <fxos/util/Timer.h>
#include <fxos/util/format.h>
Timer::Timer(): time_ns {0}, running {false}, start_time {}, depth {0}
{
}
void Timer::start(void)
{
if(this->depth++ == 0) {
this->running = true;
clock_gettime(CLOCK_REALTIME, &this->start_time);
}
}
void Timer::stop(void)
{
if(--this->depth == 0) {
this->running = false;
struct timespec end;
clock_gettime(CLOCK_REALTIME, &end);
this->time_ns += 1000000000 * (end.tv_sec - this->start_time.tv_sec)
+ (end.tv_nsec - this->start_time.tv_nsec);
this->start_time.tv_sec = 0;
this->start_time.tv_nsec = 0;
}
}
void Timer::reset(void)
{
this->time_ns = 0;
}
void Timer::restart(void)
{
this->reset();
this->start();
}
std::string Timer::format_time(uint64_t time_ns)
{
if(time_ns < 2000)
return format("%lld ns", time_ns);
time_ns /= 1000;
if(time_ns < 2000)
return format("%lld us", time_ns);
time_ns /= 1000;
if(time_ns < 2000)
return format("%lld ms", time_ns);
time_ns /= 1000;
return format("%lld s", time_ns);
}
std::string Timer::format_time() const
{
return Timer::format_time(this->time_ns);
}