oh_my_loam/common/time/timer.h

53 lines
1.0 KiB
C
Raw Permalink Normal View History

2021-01-04 21:26:09 +08:00
#pragma once
#include <chrono>
#include <string>
2021-01-05 02:09:40 +08:00
2021-01-04 21:26:09 +08:00
#include "common/macro/macros.h"
namespace common {
class Timer {
public:
2021-01-22 16:33:55 +08:00
Timer() {
Tic();
}
2021-01-04 21:26:09 +08:00
2021-02-27 17:55:29 +08:00
void Tic();
2021-01-04 21:26:09 +08:00
// unit: 's' = second, 'm' = millisecond, 'u' = microsecond
double Toc(char unit = 'm');
private:
std::chrono::time_point<std::chrono::system_clock> start_, end_;
DISALLOW_COPY_AND_ASSIGN(Timer);
};
class TimerWrapper {
public:
2021-01-22 16:33:55 +08:00
explicit TimerWrapper(const std::string &msg, double duration_ms = -1.0)
2021-01-04 21:26:09 +08:00
: msg_(msg), duration_ms_(duration_ms) {
timer_.Tic();
};
~TimerWrapper();
private:
std::string msg_;
double duration_ms_;
Timer timer_;
DISALLOW_COPY_AND_ASSIGN(TimerWrapper);
};
2021-01-05 14:33:42 +08:00
#define BLOCK_TIMER_START ::common::Timer __timer__
#define BLOCK_TIMER_STOP __timer__.Toc()
#define BLOCK_TIMER_STOP_FMT FMT_TIMESTAMP(__timer__.Toc()) << " ms"
#define BLOCK_TIME_ELAPSED(msg) ::common::TimerWrapper __timer_wrapper__(msg)
#define BLOCK_TIME_ELAPSED_EXPECT(msg, max_time) \
::common::TimerWrapper __timer_wrapper__(msg, max_time)
2021-01-04 21:26:09 +08:00
} // namespace common