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