WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
Profiler.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <chrono>
6
8
9namespace sleipnir {
10
11/**
12 * Records the number of profiler measurements (start/stop pairs) and the
13 * average duration between each start and stop call.
14 */
16 public:
17 /**
18 * Tell the profiler to start measuring setup time.
19 */
20 void StartSetup() { m_setupStartTime = std::chrono::system_clock::now(); }
21
22 /**
23 * Tell the profiler to stop measuring setup time.
24 */
25 void StopSetup() {
26 m_setupDuration = std::chrono::system_clock::now() - m_setupStartTime;
27 }
28
29 /**
30 * Tell the profiler to start measuring solve time.
31 */
32 void StartSolve() { m_solveStartTime = std::chrono::system_clock::now(); }
33
34 /**
35 * Tell the profiler to stop measuring solve time, increment the number of
36 * averages, and incorporate the latest measurement into the average.
37 */
38 void StopSolve() {
39 auto now = std::chrono::system_clock::now();
40 ++m_solveMeasurements;
41 m_averageSolveDuration =
42 (m_solveMeasurements - 1.0) / m_solveMeasurements *
43 m_averageSolveDuration +
44 1.0 / m_solveMeasurements * (now - m_solveStartTime);
45 }
46
47 /**
48 * The setup duration in milliseconds as a double.
49 */
50 double SetupDuration() const {
51 using std::chrono::duration_cast;
52 using std::chrono::nanoseconds;
53 return duration_cast<nanoseconds>(m_setupDuration).count() / 1e6;
54 }
55
56 /**
57 * The number of solve measurements taken.
58 */
59 int SolveMeasurements() const { return m_solveMeasurements; }
60
61 /**
62 * The average solve duration in milliseconds as a double.
63 */
64 double AverageSolveDuration() const {
65 using std::chrono::duration_cast;
66 using std::chrono::nanoseconds;
67 return duration_cast<nanoseconds>(m_averageSolveDuration).count() / 1e6;
68 }
69
70 private:
71 std::chrono::system_clock::time_point m_setupStartTime;
72 std::chrono::duration<double> m_setupDuration{0.0};
73
74 int m_solveMeasurements = 0;
75 std::chrono::duration<double> m_averageSolveDuration{0.0};
76 std::chrono::system_clock::time_point m_solveStartTime;
77};
78
79} // namespace sleipnir
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
Records the number of profiler measurements (start/stop pairs) and the average duration between each ...
Definition Profiler.hpp:15
void StopSolve()
Tell the profiler to stop measuring solve time, increment the number of averages, and incorporate the...
Definition Profiler.hpp:38
void StartSetup()
Tell the profiler to start measuring setup time.
Definition Profiler.hpp:20
double SetupDuration() const
The setup duration in milliseconds as a double.
Definition Profiler.hpp:50
int SolveMeasurements() const
The number of solve measurements taken.
Definition Profiler.hpp:59
double AverageSolveDuration() const
The average solve duration in milliseconds as a double.
Definition Profiler.hpp:64
void StartSolve()
Tell the profiler to start measuring solve time.
Definition Profiler.hpp:32
void StopSetup()
Tell the profiler to stop measuring setup time.
Definition Profiler.hpp:25
Definition Hessian.hpp:18