auto_timer.h Source File

CPP API: auto_timer.h Source File
auto_timer.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Rene Schmieding
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_TIMER_AUTO_TIMER_H
21 #define MIO_TIMER_AUTO_TIMER_H
22 
26 
27 namespace mio
28 {
29 namespace timing
30 {
31 
36 template <StringLiteral Name, StringLiteral Scope = "">
37 class AutoTimer
38 {
39 public:
42  : m_timer(NamedTimer<Name, Scope>::get_instance())
43  {
44  m_timer.start();
45  }
46 
49  : m_timer(timer)
50  {
51  static_assert(Name.empty() && Scope.empty(),
52  "Do not set the Name and Scope templates when using this constructor.");
53  m_timer.start();
54  }
55 
56  AutoTimer(AutoTimer&) = delete;
57  AutoTimer(AutoTimer&&) = delete;
58 
60  {
61  m_timer.stop();
62  }
63 
64 private:
66 };
67 
68 // Deduction guide that allows omitting the template parameter when using the BasicTimer constructor.
70 
71 } // namespace timing
72 } // namespace mio
73 
74 #endif // MIO_TIMER_AUTO_TIMER_H
Timer that automatically starts when it is created, and stops when it is destroyed.
Definition: auto_timer.h:38
~AutoTimer()
Definition: auto_timer.h:59
AutoTimer(AutoTimer &)=delete
AutoTimer(AutoTimer &&)=delete
BasicTimer & m_timer
Reference to the timer so it can be stopped in AutoTimer's destructor.
Definition: auto_timer.h:65
AutoTimer()
Run the NamedTimer given by the template parameter(s) Name (and Scope).
Definition: auto_timer.h:41
AutoTimer(BasicTimer &timer)
Run the given BasicTimer. Does not take ownership, so mind the timer's lifetime!
Definition: auto_timer.h:48
A minimal timer class.
Definition: basic_timer.h:36
void stop()
Stop the timer and update the elapsed time. After calling stop, the timer may be started again.
Definition: basic_timer.h:47
void start()
Start the timer. Must be followed by exactly one stop.
Definition: basic_timer.h:39
Thread local singleton timer, identified by its name. Best used via AutoTimer.
Definition: named_timer.h:38
AutoTimer(BasicTimer &timer) -> AutoTimer<"">
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
Wrapper for string literals, that allows passing them as template arguments. Should be used with cons...
Definition: string_literal.h:32
constexpr bool empty() const
Check whether the StringLiteral is empty.
Definition: string_literal.h:59