hpp-fcl  1.7.0
HPP fork of FCL -- The Flexible Collision Library
timings.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2021 INRIA
3 //
4 
5 #ifndef HPP_FCL_TIMINGS_FWD_H
6 #define HPP_FCL_TIMINGS_FWD_H
7 
8 #include <boost/chrono.hpp>
9 
10 #include "hpp/fcl/fwd.hh"
11 
12 namespace hpp { namespace fcl {
13 
14  struct CPUTimes
15  {
19 
21  : wall(0)
22  , user(0)
23  , system(0)
24  {}
25 
26  void clear()
27  {
28  wall = user = system = 0;
29  }
30 
31  };
32 
33  namespace internal
34  {
35  inline void get_cpu_times(CPUTimes & current)
36  {
37  using namespace boost::chrono;
38 
39  process_real_cpu_clock::time_point wall = process_real_cpu_clock::now();
40  process_user_cpu_clock::time_point user = process_user_cpu_clock::now();
41  process_system_cpu_clock::time_point system = process_system_cpu_clock::now();
42 
43  current.wall = time_point_cast<nanoseconds>(wall).time_since_epoch().count()*1e-3;
44  current.user = time_point_cast<nanoseconds>(user).time_since_epoch().count()*1e-3;
45  current.system = time_point_cast<nanoseconds>(system).time_since_epoch().count()*1e-3;
46  }
47  }
48 
52  struct Timer
53  {
54 
56  {
57  start();
58  }
59 
60  CPUTimes elapsed() const
61  {
62  if(m_is_stopped)
63  return m_times;
64 
65  CPUTimes current;
66  internal::get_cpu_times(current);
67  current.wall -= m_times.wall;
68  current.user -= m_times.user;
69  current.system -= m_times.system;
70  return current;
71  }
72 
73  void start()
74  {
75  m_is_stopped = false;
76  internal::get_cpu_times(m_times);
77  }
78 
79  void stop()
80  {
81  if(m_is_stopped)
82  return;
83  m_is_stopped = true;
84 
85  CPUTimes current;
86  internal::get_cpu_times(current);
87  m_times.wall = (current.wall - m_times.wall);
88  m_times.user = (current.user - m_times.user);
89  m_times.system = (current.system - m_times.system);
90 
91  }
92 
93  void resume()
94  {
95  if(m_is_stopped)
96  {
97  CPUTimes current(m_times);
98  start();
99  m_times.wall -= current.wall;
100  m_times.user -= current.user;
101  m_times.system -= current.system;
102  }
103  }
104 
105  bool is_stopped() const
106  {
107  return m_is_stopped;
108  }
109 
110  protected:
111 
114  };
115 
116 }}
117 
118 #endif // ifndef HPP_FCL_TIMINGS_FWD_H
FCL_REAL user
Definition: timings.h:17
CPUTimes()
Definition: timings.h:20
CPUTimes elapsed() const
Definition: timings.h:60
Main namespace.
Definition: AABB.h:43
void clear()
Definition: timings.h:26
bool is_stopped() const
Definition: timings.h:105
void get_cpu_times(CPUTimes &current)
Definition: timings.h:35
double FCL_REAL
Definition: data_types.h:66
Definition: timings.h:14
CPUTimes m_times
Definition: timings.h:112
This class mimics the way "boost/timer/timer.hpp" operates while using moder boost::chrono library...
Definition: timings.h:52
bool m_is_stopped
Definition: timings.h:113
Timer()
Definition: timings.h:55
FCL_REAL system
Definition: timings.h:18
FCL_REAL wall
Definition: timings.h:16
void stop()
Definition: timings.h:79
void resume()
Definition: timings.h:93
void start()
Definition: timings.h:73