Bench-MR
A Motion Planning Benchmark for Wheeled Mobile Robots
Stopwatch.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4
8class Stopwatch {
9 typedef std::chrono::high_resolution_clock clock_t;
10
11 public:
15 void start() {
16 start_ = clock_t::now();
17 running_ = true;
18 }
19
23 void reset() {
24 elapsed_ = 0.;
25 running_ = false;
26 }
27
32 double stop() {
33 elapsed_ = calcElapsed_();
34 running_ = false;
35 return elapsed_;
36 }
37
41 double elapsed() const {
42 if (!running_)
43 return elapsed_;
44 else
45 return calcElapsed_();
46 }
47
51 void resume() {
52 start_ = clock_t::now() -
53 std::chrono::microseconds(static_cast<long>(elapsed_ * 1e6));
54 running_ = true;
55 }
56
57 protected:
58 double elapsed_{0};
59 bool running_{false};
60
61 private:
62 std::chrono::time_point<clock_t> start_;
63
64 double calcElapsed_() const {
65 const auto end = clock_t::now();
66 return std::chrono::duration_cast<std::chrono::microseconds>(end - start_)
67 .count() /
68 1e6;
69 }
70};
Stopwatch implementation to measure elapsed time.
Definition: Stopwatch.hpp:8
bool running_
Definition: Stopwatch.hpp:59
void reset()
Stops the timer and sets elapsed time to zero.
Definition: Stopwatch.hpp:23
double elapsed_
Definition: Stopwatch.hpp:58
double stop()
Stops the timer.
Definition: Stopwatch.hpp:32
void start()
Starts the timer.
Definition: Stopwatch.hpp:15
void resume()
Continues the stop watch from when it was stopped.
Definition: Stopwatch.hpp:51
double elapsed() const
Elapsed time in seconds.
Definition: Stopwatch.hpp:41