proxsuite 0.6.7
The Advanced Proximal Optimization Toolbox
Loading...
Searching...
No Matches
timings.hpp
Go to the documentation of this file.
1//
2// Copyright (c) 2022 INRIA
3//
4
5#ifndef PROXSUITE_PROXQP_TIMINGS_HPP
6#define PROXSUITE_PROXQP_TIMINGS_HPP
7
8#include <chrono>
9
10namespace proxsuite {
11namespace proxqp {
12
14{
15 double wall;
16 double user;
17 double system;
18
20 : wall(0)
21 , user(0)
22 , system(0)
23 {
24 }
25
26 void clear() { wall = user = system = 0; }
27};
28
34template<typename T>
35struct Timer
36{
38 : m_is_stopped(true)
39 {
40 start();
41 }
42
44 {
45 if (m_is_stopped)
46 return m_times;
47
48 CPUTimes current(m_times);
49 std::chrono::time_point<std::chrono::steady_clock> current_clock =
50 std::chrono::steady_clock::now();
51 current.user +=
52 static_cast<T>(std::chrono::duration_cast<std::chrono::nanoseconds>(
53 current_clock - m_start)
54 .count()) *
55 1e-3;
56
57 return current;
58 }
59
60 void start()
61 {
62 if (m_is_stopped) {
63 m_is_stopped = false;
64 m_times.clear();
65 m_start = std::chrono::steady_clock::now();
66 }
67 }
68
69 void stop()
70 {
71 if (m_is_stopped)
72 return;
73 m_is_stopped = true;
74
75 m_end = std::chrono::steady_clock::now();
76 m_times.user +=
77 static_cast<double>(
78 std::chrono::duration_cast<std::chrono::nanoseconds>(m_end - m_start)
79 .count()) *
80 1e-3;
81 }
82
83 void resume()
84 {
85 if (m_is_stopped)
86 m_start = std::chrono::steady_clock::now();
87 }
88
89 bool is_stopped() const { return m_is_stopped; }
90
91protected:
94
95 std::chrono::time_point<std::chrono::steady_clock> m_start, m_end;
96};
97
98} // namespace proxqp
99} // namespace proxsuite
100
101#endif // ifndef PROXSUITE_PROXQP_TIMINGS_HPP
This class mimics the way "boost/timer/timer.hpp" operates while using the modern std::chrono library...
Definition timings.hpp:36
std::chrono::time_point< std::chrono::steady_clock > m_end
Definition timings.hpp:95
CPUTimes elapsed() const
Definition timings.hpp:43
std::chrono::time_point< std::chrono::steady_clock > m_start
Definition timings.hpp:95
bool is_stopped() const
Definition timings.hpp:89