头文件
#include <chrono>
#include <functional>namespace hsm {
namespace common {class Timer {
public:Timer();void reset();long peek_us() const;long peek_ms() const;double peek_msf() const;double record_msf(const std::function<void()> &func);private:std::chrono::system_clock::time_point start;
};} // namespace common
} // namespace hsm
对应实现
#include "common/timer.h"namespace hsm {
namespace common {using namespace std::chrono;Timer::Timer() { reset(); };void Timer::reset() { start = system_clock::now(); }long Timer::peek_us() const {return duration_cast<microseconds>(system_clock::now() - start).count();
}long Timer::peek_ms() const {return duration_cast<milliseconds>(system_clock::now() - start).count();
}double Timer::peek_msf() const { return static_cast<double>(peek_us()) / 1e3; }double Timer::record_msf(const std::function<void()> &func) {reset();func();return peek_msf();
}} // namespace common
} // namespace hsm