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
14template <typename Profiler>
15 requires std::same_as<Profiler, SetupProfiler> ||
16 std::same_as<Profiler, SolveProfiler>
18 public:
22 explicit ScopedProfiler(Profiler& profiler) noexcept : m_profiler{&profiler} {
23 m_profiler->start();
24 }
25
28 if (m_active) {
29 m_profiler->stop();
30 }
31 }
32
37 : m_profiler{std::move(rhs.m_profiler)}, m_active{rhs.m_active} {
38 rhs.m_active = false;
39 }
40
41 ScopedProfiler(const ScopedProfiler&) = delete;
42 ScopedProfiler& operator=(const ScopedProfiler&) = delete;
43
47 void stop() {
48 if (m_active) {
49 m_profiler->stop();
50 m_active = false;
51 }
52 }
53
57 const std::chrono::duration<double>& current_duration() const {
58 return m_profiler->current_duration();
59 }
60
61 private:
62 Profiler* m_profiler;
63 bool m_active = true;
64};
65
66} // namespace slp
Definition intrusive_shared_ptr.hpp:27
Starts a profiler in the constructor and stops it in the destructor.
Definition scoped_profiler.hpp:17
ScopedProfiler(ScopedProfiler &&rhs) noexcept
Definition scoped_profiler.hpp:36
void stop()
Definition scoped_profiler.hpp:47
~ScopedProfiler()
Stops a profiler.
Definition scoped_profiler.hpp:27
ScopedProfiler(Profiler &profiler) noexcept
Definition scoped_profiler.hpp:22
const std::chrono::duration< double > & current_duration() const
Definition scoped_profiler.hpp:57