Sleipnir C++ API
Loading...
Searching...
No Matches
scoped_profiler.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <concepts>
6#include <utility>
7
8#include "sleipnir/util/setup_profiler.hpp"
9#include "sleipnir/util/solve_profiler.hpp"
10
11namespace slp {
12
16template <typename Profiler>
17 requires std::same_as<Profiler, SetupProfiler> ||
18 std::same_as<Profiler, SolveProfiler>
20 public:
26 explicit ScopedProfiler(Profiler& profiler) noexcept : m_profiler{&profiler} {
27 m_profiler->start();
28 }
29
34 if (m_active) {
35 m_profiler->stop();
36 }
37 }
38
45 : m_profiler{std::move(rhs.m_profiler)}, m_active{rhs.m_active} {
46 rhs.m_active = false;
47 }
48
49 ScopedProfiler(const ScopedProfiler&) = delete;
50 ScopedProfiler& operator=(const ScopedProfiler&) = delete;
51
57 void stop() {
58 if (m_active) {
59 m_profiler->stop();
60 m_active = false;
61 }
62 }
63
69 const std::chrono::duration<double>& current_duration() const {
70 return m_profiler->current_duration();
71 }
72
73 private:
74 Profiler* m_profiler;
75 bool m_active = true;
76};
77
78} // namespace slp
Definition scoped_profiler.hpp:19
ScopedProfiler(ScopedProfiler &&rhs) noexcept
Definition scoped_profiler.hpp:44
void stop()
Definition scoped_profiler.hpp:57
~ScopedProfiler()
Definition scoped_profiler.hpp:33
ScopedProfiler(Profiler &profiler) noexcept
Definition scoped_profiler.hpp:26
const std::chrono::duration< double > & current_duration() const
Definition scoped_profiler.hpp:69