Sleipnir C++ API
Loading...
Searching...
No Matches
profiler.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <chrono>
6#include <concepts>
7#include <string>
8#include <string_view>
9#include <utility>
10
11namespace slp {
12
16 public:
20 explicit SetupProfiler(std::string_view name) : m_name{name} {}
21
23 void start() {
24#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
25 m_setup_start_time = std::chrono::steady_clock::now();
26#endif
27 }
28
30 void stop() {
31#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
32 m_setup_stop_time = std::chrono::steady_clock::now();
33 m_setup_duration = m_setup_stop_time - m_setup_start_time;
34#endif
35 }
36
40 std::string_view name() const { return m_name; }
41
45 const std::chrono::duration<double>& duration() const {
46 return m_setup_duration;
47 }
48
49 private:
51 std::string m_name;
52
53 std::chrono::steady_clock::time_point m_setup_start_time;
54 std::chrono::steady_clock::time_point m_setup_stop_time;
55 std::chrono::duration<double> m_setup_duration{0.0};
56};
57
61 public:
65 explicit SolveProfiler(std::string_view name) : m_name{name} {}
66
68 void start() {
69#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
70 m_current_solve_start_time = std::chrono::steady_clock::now();
71#endif
72 }
73
76 void stop() {
77#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
78 m_current_solve_stop_time = std::chrono::steady_clock::now();
79 m_current_solve_duration =
80 m_current_solve_stop_time - m_current_solve_start_time;
81 m_total_solve_duration += m_current_solve_duration;
82
83 ++m_num_solves;
84 m_average_solve_duration =
85 (m_num_solves - 1.0) / m_num_solves * m_average_solve_duration +
86 1.0 / m_num_solves * m_current_solve_duration;
87#endif
88 }
89
93 std::string_view name() const { return m_name; }
94
98 int num_solves() const { return m_num_solves; }
99
103 const std::chrono::duration<double>& current_duration() const {
104 return m_current_solve_duration;
105 }
106
110 const std::chrono::duration<double>& average_duration() const {
111 return m_average_solve_duration;
112 }
113
117 const std::chrono::duration<double>& total_duration() const {
118 return m_total_solve_duration;
119 }
120
121 private:
123 std::string m_name;
124
125 std::chrono::steady_clock::time_point m_current_solve_start_time;
126 std::chrono::steady_clock::time_point m_current_solve_stop_time;
127 std::chrono::duration<double> m_current_solve_duration{0.0};
128 std::chrono::duration<double> m_total_solve_duration{0.0};
129
130 int m_num_solves = 0;
131 std::chrono::duration<double> m_average_solve_duration{0.0};
132};
133
135template <typename Profiler>
136 requires std::same_as<Profiler, SetupProfiler> ||
137 std::same_as<Profiler, SolveProfiler>
139 public:
144 m_profiler->start();
145 }
146
149 if (m_active) {
150 m_profiler->stop();
151 }
152 }
153
158 : m_profiler{std::move(rhs.m_profiler)}, m_active{rhs.m_active} {
159 rhs.m_active = false;
160 }
161
162 ScopedProfiler(const ScopedProfiler&) = delete;
163 ScopedProfiler& operator=(const ScopedProfiler&) = delete;
164
168 void stop() {
169 if (m_active) {
170 m_profiler->stop();
171 m_active = false;
172 }
173 }
174
178 const std::chrono::duration<double>& current_duration() const {
179 return m_profiler->current_duration();
180 }
181
182 private:
183 Profiler* m_profiler;
184 bool m_active = true;
185};
186
187} // namespace slp
Definition intrusive_shared_ptr.hpp:27
Starts a profiler in the constructor and stops it in the destructor.
Definition profiler.hpp:138
ScopedProfiler(ScopedProfiler &&rhs) noexcept
Definition profiler.hpp:157
void stop()
Definition profiler.hpp:168
~ScopedProfiler()
Stops a profiler.
Definition profiler.hpp:148
ScopedProfiler(Profiler &profiler) noexcept
Definition profiler.hpp:143
const std::chrono::duration< double > & current_duration() const
Definition profiler.hpp:178
Definition profiler.hpp:15
const std::chrono::duration< double > & duration() const
Definition profiler.hpp:45
SetupProfiler(std::string_view name)
Definition profiler.hpp:20
std::string_view name() const
Definition profiler.hpp:40
void stop()
Tell the profiler to stop measuring setup time.
Definition profiler.hpp:30
void start()
Tell the profiler to start measuring setup time.
Definition profiler.hpp:23
Definition profiler.hpp:60
const std::chrono::duration< double > & average_duration() const
Definition profiler.hpp:110
const std::chrono::duration< double > & current_duration() const
Definition profiler.hpp:103
SolveProfiler(std::string_view name)
Definition profiler.hpp:65
const std::chrono::duration< double > & total_duration() const
Definition profiler.hpp:117
std::string_view name() const
Definition profiler.hpp:93
void start()
Tell the profiler to start measuring solve time.
Definition profiler.hpp:68
int num_solves() const
Definition profiler.hpp:98
void stop()
Definition profiler.hpp:76