Debian Codegraph @

examples/simulations/gray-scott/common/timer.hpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7
8 #ifndef __TIMER_HPP__
9 #define __TIMER_HPP__
10
11 #include <chrono>
12
13 class Timer
14 {
15 private:
16 bool _is_running;
17 std::chrono::steady_clock::time_point _start;
18 std::chrono::steady_clock::duration _total;
19
20 double _to_millis(std::chrono::steady_clock::duration duration) const
21 {
22 std::chrono::duration<double, std::milli> duration_ms(duration);
23 return duration_ms.count();
24 }
25
26 public:
27 Timer() : _is_running(false), _total(std::chrono::steady_clock::duration::zero()) {}
28
29 void start()
30 {
31 _is_running = true;
32 _start = std::chrono::steady_clock::now();
33 }
34
35 double stop()
36 {
37 _is_running = false;
38
39 const auto elapsed = std::chrono::steady_clock::now() - _start;
40
41 _total += elapsed;
42
43 return _to_millis(elapsed);
44 }
45
46 void reset()
47 {
48 _is_running = false;
49 _total = std::chrono::steady_clock::duration::zero();
50 }
51
52 bool is_running() const { return _is_running; }
53
54 double elapsed() const { return _to_millis(_total); }
55 };
56
57 #endif