GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/hpp/fcl/timings.h Lines: 19 29 65.5 %
Date: 2024-02-09 12:57:42 Branches: 4 14 28.6 %

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