coal 3.0.2
Coal, The Collision Detection Library. Previously known as HPP-FCL, fork of FCL -- The Flexible Collision Library
Loading...
Searching...
No Matches
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
14namespace coal {
15
16struct 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
31struct 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
Main namespace.
Definition broadphase_bruteforce.h:44
Definition timings.h:16
double user
Definition timings.h:18
double wall
Definition timings.h:17
CPUTimes()
Definition timings.h:21
double system
Definition timings.h:19
void clear()
Definition timings.h:23
void resume()
Definition timings.h:90
Timer(const bool start_on_construction=true)
Default constructor for the timer.
Definition timings.h:41
void stop()
Definition timings.h:76
bool m_is_stopped
Definition timings.h:103
CPUTimes m_times
Definition timings.h:102
bool is_stopped() const
Definition timings.h:99
void start()
Definition timings.h:65
CPUTimes elapsed() const
Definition timings.h:45