WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
solve_profiler.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <chrono>
6#include <string>
7#include <string_view>
8
9namespace slp {
10
11/// Records the number of profiler measurements (start/stop pairs) and the
12/// average duration between each start and stop call.
14 public:
15 /// Constructs a SolveProfiler.
16 ///
17 /// @param name Name of measurement to show in diagnostics.
18 explicit SolveProfiler(std::string_view name) : m_name{name} {}
19
20 /// Tell the profiler to start measuring solve time.
21 void start() {
22#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
23 m_current_solve_start_time = std::chrono::steady_clock::now();
24#endif
25 }
26
27 /// Tell the profiler to stop measuring solve time, increment the number of
28 /// averages, and incorporate the latest measurement into the average.
29 void stop() {
30#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
31 m_current_solve_stop_time = std::chrono::steady_clock::now();
32 m_current_solve_duration =
33 m_current_solve_stop_time - m_current_solve_start_time;
34 m_total_solve_duration += m_current_solve_duration;
35
36 ++m_num_solves;
37 m_average_solve_duration =
38 (m_num_solves - 1.0) / m_num_solves * m_average_solve_duration +
39 1.0 / m_num_solves * m_current_solve_duration;
40#endif
41 }
42
43 /// Returns name of measurement to show in diagnostics.
44 ///
45 /// @return Name of measurement to show in diagnostics.
46 std::string_view name() const { return m_name; }
47
48 /// Returns the number of solves.
49 ///
50 /// @return The number of solves.
51 int num_solves() const { return m_num_solves; }
52
53 /// Returns the most recent solve duration in seconds.
54 ///
55 /// @return The most recent solve duration in seconds.
56 const std::chrono::duration<double>& current_duration() const {
57 return m_current_solve_duration;
58 }
59
60 /// Returns the average solve duration in seconds.
61 ///
62 /// @return The average solve duration in seconds.
63 const std::chrono::duration<double>& average_duration() const {
64 return m_average_solve_duration;
65 }
66
67 /// Returns the sum of all solve durations in seconds.
68 ///
69 /// @return The sum of all solve durations in seconds.
70 const std::chrono::duration<double>& total_duration() const {
71 return m_total_solve_duration;
72 }
73
74 private:
75 /// Name of measurement to show in diagnostics.
76 std::string m_name;
77
78 std::chrono::steady_clock::time_point m_current_solve_start_time;
79 std::chrono::steady_clock::time_point m_current_solve_stop_time;
80 std::chrono::duration<double> m_current_solve_duration{0.0};
81 std::chrono::duration<double> m_total_solve_duration{0.0};
82
83 int m_num_solves = 0;
84 std::chrono::duration<double> m_average_solve_duration{0.0};
85};
86
87} // namespace slp
const std::chrono::duration< double > & average_duration() const
Returns the average solve duration in seconds.
Definition solve_profiler.hpp:63
const std::chrono::duration< double > & current_duration() const
Returns the most recent solve duration in seconds.
Definition solve_profiler.hpp:56
SolveProfiler(std::string_view name)
Constructs a SolveProfiler.
Definition solve_profiler.hpp:18
const std::chrono::duration< double > & total_duration() const
Returns the sum of all solve durations in seconds.
Definition solve_profiler.hpp:70
std::string_view name() const
Returns name of measurement to show in diagnostics.
Definition solve_profiler.hpp:46
void start()
Tell the profiler to start measuring solve time.
Definition solve_profiler.hpp:21
int num_solves() const
Returns the number of solves.
Definition solve_profiler.hpp:51
void stop()
Tell the profiler to stop measuring solve time, increment the number of averages, and incorporate the...
Definition solve_profiler.hpp:29
Definition expression_graph.hpp:11