coal  3.0.1
Coal, The Collision Detection Library. Previously known as HPP-FCL, fork of FCL -- The Flexible Collision Library
timings.h
Go to the documentation of this file.
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  CPUTimes() : wall(0), user(0), system(0) {}
20 
21  void clear() { wall = user = system = 0; }
22 };
23 
30  typedef std::chrono::steady_clock clock_type;
31  typedef clock_type::duration duration_type;
32 
37  Timer(const bool start_on_construction = true) : m_is_stopped(true) {
38  if (start_on_construction) Timer::start();
39  }
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  void start() {
58  if (m_is_stopped) {
59  m_is_stopped = false;
60  m_times.clear();
61 
62  m_start = std::chrono::steady_clock::now();
63  }
64  }
65 
66  void stop() {
67  if (m_is_stopped) return;
68  m_is_stopped = true;
69 
70  m_end = std::chrono::steady_clock::now();
71  m_times.user += static_cast<double>(
72  std::chrono::duration_cast<std::chrono::nanoseconds>(
73  m_end - m_start)
74  .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:
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
#define COAL_DLLAPI
Definition: config.hh:88
Main namespace.
Definition: broadphase_bruteforce.h:44
Definition: timings.h:14
double user
Definition: timings.h:16
double wall
Definition: timings.h:15
CPUTimes()
Definition: timings.h:19
double system
Definition: timings.h:17
void clear()
Definition: timings.h:21
This class mimics the way "boost/timer/timer.hpp" operates while using the modern std::chrono library...
Definition: timings.h:29
void resume()
Definition: timings.h:78
std::chrono::time_point< std::chrono::steady_clock > m_end
Definition: timings.h:91
duration_type duration() const
Definition: timings.h:55
Timer(const bool start_on_construction=true)
Default constructor for the timer.
Definition: timings.h:37
clock_type::duration duration_type
Definition: timings.h:31
void stop()
Definition: timings.h:66
bool m_is_stopped
Definition: timings.h:89
CPUTimes m_times
Definition: timings.h:88
bool is_stopped() const
Definition: timings.h:85
void start()
Definition: timings.h:57
std::chrono::steady_clock clock_type
Definition: timings.h:30
CPUTimes elapsed() const
Definition: timings.h:41