oh_my_loam/common/time/timer.cc

29 lines
825 B
C++
Raw Normal View History

2021-01-04 21:26:09 +08:00
#include "timer.h"
2021-01-05 02:09:40 +08:00
2021-01-04 21:26:09 +08:00
#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) {
2021-01-05 02:09:40 +08:00
AINFO << msg_ << ": time elapsed: " << FMT_TIMESTAMP(duration) << " ms";
2021-01-04 21:26:09 +08:00
}
if (duration_ms_ > 0 && duration > duration_ms_) {
2021-01-05 02:09:40 +08:00
AWARN << msg_ << ": time elapsed: " << FMT_TIMESTAMP(duration) << " ms";
2021-01-04 21:26:09 +08:00
}
}
} // namespace common