oh_my_loam/common/time/timer.cc

28 lines
816 B
C++
Raw Normal View History

2021-01-04 21:26:09 +08:00
#include "timer.h"
#include "common/log/log.h"
namespace common {
double Timer::Toc(char unit) {
ACHECK(unit == 's' || unit == 'm' || unit == 'u')
<< "Only 's'(second), 'm'(millisecond) and 'u'(microsecond) are "
"supported";
double factor = 1.0;
if (unit == 'm') factor = 1.0e3;
if (unit == 'u') factor = 1.0e6;
end_ = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end_ - start_;
return elapsed_seconds.count() * factor;
}
TimerWrapper::~TimerWrapper() {
double duration = timer_.Toc();
if (duration_ms_ < 0) {
AINFO << msg_ << ": time elapsed (ms): " << FMT_TIMESTAMP(duration);
}
if (duration_ms_ > 0 && duration > duration_ms_) {
AWARN << msg_ << ": time elapsed (ms): " << FMT_TIMESTAMP(duration);
}
}
} // namespace common