coal/timings.h
Go to the documentation of this file.
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  CPUTimes() : wall(0), user(0), system(0) {}
22 
23  void clear() { wall = user = system = 0; }
24 };
25 
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 
41  Timer(const bool start_on_construction = true) : m_is_stopped(true) {
42  if (start_on_construction) Timer::start();
43  }
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  void start() {
66  if (m_is_stopped) {
67  m_is_stopped = false;
68  m_times.clear();
69 
70 #ifdef COAL_WITH_CXX11_SUPPORT
71  m_start = std::chrono::steady_clock::now();
72 #endif
73  }
74  }
75 
76  void stop() {
77  if (m_is_stopped) return;
78  m_is_stopped = true;
79 
80 #ifdef COAL_WITH_CXX11_SUPPORT
81  m_end = std::chrono::steady_clock::now();
82  m_times.user += static_cast<double>(
83  std::chrono::duration_cast<std::chrono::nanoseconds>(
84  m_end - m_start)
85  .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:
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
coal::CPUTimes
Definition: coal/timings.h:16
coal::Timer::stop
void stop()
Definition: coal/timings.h:76
coal::CPUTimes::system
double system
Definition: coal/timings.h:19
duration_type
clock_type::duration duration_type
Definition: obb.cpp:90
coal::Timer
This class mimics the way "boost/timer/timer.hpp" operates while using the modern std::chrono library...
Definition: coal/timings.h:31
coal::CPUTimes::user
double user
Definition: coal/timings.h:18
coal
Main namespace.
Definition: coal/broadphase/broadphase_bruteforce.h:44
coal::CPUTimes::clear
void clear()
Definition: coal/timings.h:23
coal::CPUTimes::CPUTimes
CPUTimes()
Definition: coal/timings.h:21
coal::CPUTimes::wall
double wall
Definition: coal/timings.h:17
fwd.hh
coal::Timer::start
void start()
Definition: coal/timings.h:65
coal::Timer::m_times
CPUTimes m_times
Definition: coal/timings.h:102
coal::Timer::resume
void resume()
Definition: coal/timings.h:90
coal::Timer::m_is_stopped
bool m_is_stopped
Definition: coal/timings.h:103
coal::Timer::Timer
Timer(const bool start_on_construction=true)
Default constructor for the timer.
Definition: coal/timings.h:41
clock_type
std::chrono::high_resolution_clock clock_type
Definition: obb.cpp:89
coal::Timer::elapsed
CPUTimes elapsed() const
Definition: coal/timings.h:45
coal::Timer::is_stopped
bool is_stopped() const
Definition: coal/timings.h:99


hpp-fcl
Author(s):
autogenerated on Sat Nov 23 2024 03:44:59