Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Utils {
- template <
- typename TimeUnit = std::chrono::milliseconds,
- typename Clock = std::chrono::high_resolution_clock
- >
- class Stopwatch
- {
- public:
- explicit Stopwatch()
- {
- reset();
- }
- inline void reset()
- {
- start_ = Clock::now();
- stopped_ = false;
- }
- inline void stop()
- {
- stop_ = Clock::now();
- stopped_ = true;
- }
- /// If the timer is stopped, returns the elapsed time between the start and stop times.
- /// Otherwise, returns the elapsed time between the start time and the current time
- inline TimeUnit elapsed() const
- {
- auto t = stopped_ ? stop_ : Clock::now();
- return std::chrono::duration_cast<TimeUnit>(t - start_);
- }
- private:
- std::chrono::time_point<Clock> start_, stop_;
- bool stopped_ = false;
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement