2017-03-25 19:18:25 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-10-20 20:56:26 +00:00
|
|
|
#include <optional.h>
|
|
|
|
|
2017-03-25 19:18:25 +00:00
|
|
|
#include <chrono>
|
2017-03-31 04:13:58 +00:00
|
|
|
#include <string>
|
2017-03-25 19:18:25 +00:00
|
|
|
|
|
|
|
struct Timer {
|
|
|
|
using Clock = std::chrono::high_resolution_clock;
|
|
|
|
|
|
|
|
// Creates a new timer. A timer is always running.
|
|
|
|
Timer();
|
|
|
|
|
2017-05-17 07:08:45 +00:00
|
|
|
// Returns elapsed microseconds.
|
|
|
|
long long ElapsedMicroseconds() const;
|
|
|
|
// Returns elapsed microseconds and restarts/resets the timer.
|
|
|
|
long long ElapsedMicrosecondsAndReset();
|
2017-03-25 19:18:25 +00:00
|
|
|
// Restart/reset the timer.
|
|
|
|
void Reset();
|
2017-03-31 04:13:58 +00:00
|
|
|
// Resets timer and prints a message like "<foo> took 5ms"
|
|
|
|
void ResetAndPrint(const std::string& message);
|
2017-10-20 20:56:26 +00:00
|
|
|
// Pause the timer.
|
|
|
|
void Pause();
|
|
|
|
// Resume the timer after it has been paused.
|
|
|
|
void Resume();
|
2017-03-25 19:18:25 +00:00
|
|
|
|
|
|
|
// Raw start time.
|
2017-10-20 20:56:26 +00:00
|
|
|
optional<std::chrono::time_point<Clock>> start_;
|
|
|
|
// Elapsed time.
|
|
|
|
long long elapsed_ = 0;
|
2017-03-25 19:18:25 +00:00
|
|
|
};
|
2018-01-12 18:01:35 +00:00
|
|
|
|
|
|
|
struct ScopedPerfTimer {
|
|
|
|
ScopedPerfTimer(const std::string& message);
|
|
|
|
~ScopedPerfTimer();
|
|
|
|
|
|
|
|
Timer timer_;
|
|
|
|
std::string message_;
|
|
|
|
};
|