1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <chrono>
#include <cstdio>
class Timer {
public:
using Clock = std::chrono::steady_clock;
Timer(bool enabled) : enabled_(enabled) {}
void start() {
if (enabled_)
start_time_ = Clock::now();
}
bool enabled() const { return enabled_; }
double count() const {
std::chrono::duration<double> elapsed = Clock::now() - start_time_;
return elapsed.count();
}
void print(const char* msg) const {
if (enabled_)
std::fprintf(stderr, "%s %g s.\n", msg, count());
}
private:
bool enabled_;
std::chrono::time_point<Clock> start_time_;
};
|