Directory: | ./ |
---|---|
File: | include/coal/timings.h |
Date: | 2025-04-01 09:23:31 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 19 | 29 | 65.5% |
Branches: | 4 | 14 | 28.6% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // | ||
2 | // Copyright (c) 2021-2023 INRIA | ||
3 | // | ||
4 | |||
5 | #ifndef COAL_TIMINGS_FWD_H | ||
6 | #define COAL_TIMINGS_FWD_H | ||
7 | |||
8 | #include "coal/fwd.hh" | ||
9 | |||
10 | #ifdef COAL_WITH_CXX11_SUPPORT | ||
11 | #include <chrono> | ||
12 | #endif | ||
13 | |||
14 | namespace coal { | ||
15 | |||
16 | struct CPUTimes { | ||
17 | double wall; | ||
18 | double user; | ||
19 | double system; | ||
20 | |||
21 | 29280675 | CPUTimes() : wall(0), user(0), system(0) {} | |
22 | |||
23 | 38992 | void clear() { wall = user = system = 0; } | |
24 | }; | ||
25 | |||
26 | /// | ||
27 | /// @brief This class mimics the way "boost/timer/timer.hpp" operates while | ||
28 | /// using the modern std::chrono library. | ||
29 | /// Importantly, this class will only have an effect for C++11 and more. | ||
30 | /// | ||
31 | struct COAL_DLLAPI Timer { | ||
32 | #ifdef COAL_WITH_CXX11_SUPPORT | ||
33 | typedef std::chrono::steady_clock clock_type; | ||
34 | typedef clock_type::duration duration_type; | ||
35 | #endif | ||
36 | |||
37 | /// \brief Default constructor for the timer | ||
38 | /// | ||
39 | /// \param[in] start_on_construction if true, the timer will be run just after | ||
40 | /// the object is created | ||
41 | 69 | Timer(const bool start_on_construction = true) : m_is_stopped(true) { | |
42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
|
69 | if (start_on_construction) Timer::start(); |
43 | 69 | } | |
44 | |||
45 | ✗ | CPUTimes elapsed() const { | |
46 | ✗ | if (m_is_stopped) return m_times; | |
47 | |||
48 | ✗ | CPUTimes current(m_times); | |
49 | #ifdef COAL_WITH_CXX11_SUPPORT | ||
50 | std::chrono::time_point<std::chrono::steady_clock> current_clock = | ||
51 | ✗ | std::chrono::steady_clock::now(); | |
52 | ✗ | current.user += static_cast<double>( | |
53 | ✗ | std::chrono::duration_cast<std::chrono::nanoseconds>( | |
54 | ✗ | current_clock - m_start) | |
55 | ✗ | .count()) * | |
56 | 1e-3; | ||
57 | #endif | ||
58 | ✗ | return current; | |
59 | } | ||
60 | |||
61 | #ifdef COAL_WITH_CXX11_SUPPORT | ||
62 | ✗ | duration_type duration() const { return (m_end - m_start); } | |
63 | #endif | ||
64 | |||
65 | 45 | void start() { | |
66 |
1/2✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
|
45 | if (m_is_stopped) { |
67 | 45 | m_is_stopped = false; | |
68 | 45 | m_times.clear(); | |
69 | |||
70 | #ifdef COAL_WITH_CXX11_SUPPORT | ||
71 | 45 | m_start = std::chrono::steady_clock::now(); | |
72 | #endif | ||
73 | } | ||
74 | 45 | } | |
75 | |||
76 | 45 | void stop() { | |
77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
|
45 | if (m_is_stopped) return; |
78 | 45 | m_is_stopped = true; | |
79 | |||
80 | #ifdef COAL_WITH_CXX11_SUPPORT | ||
81 | 45 | m_end = std::chrono::steady_clock::now(); | |
82 | 45 | m_times.user += static_cast<double>( | |
83 |
1/2✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
|
45 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
84 | 45 | m_end - m_start) | |
85 | 45 | .count()) * | |
86 | 1e-3; | ||
87 | #endif | ||
88 | } | ||
89 | |||
90 | void resume() { | ||
91 | #ifdef COAL_WITH_CXX11_SUPPORT | ||
92 | if (m_is_stopped) { | ||
93 | m_start = std::chrono::steady_clock::now(); | ||
94 | m_is_stopped = false; | ||
95 | } | ||
96 | #endif | ||
97 | } | ||
98 | |||
99 | bool is_stopped() const { return m_is_stopped; } | ||
100 | |||
101 | protected: | ||
102 | CPUTimes m_times; | ||
103 | bool m_is_stopped; | ||
104 | |||
105 | #ifdef COAL_WITH_CXX11_SUPPORT | ||
106 | std::chrono::time_point<std::chrono::steady_clock> m_start, m_end; | ||
107 | #endif | ||
108 | }; | ||
109 | |||
110 | } // namespace coal | ||
111 | |||
112 | #endif // ifndef COAL_TIMINGS_FWD_H | ||
113 |