HighMap library (C++)
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
16#pragma once
17
18#include <chrono>
19#include <iomanip>
20#include <iostream>
21#include <list>
22#include <map>
23#include <string>
24
25namespace hmap
26{
27
33{
34public:
40 Recorder(std::string name);
41
45 void dump();
46
50 void start();
51
56 void stop();
57
58 std::string name;
59 int nb_calls = 0;
60 std::chrono::high_resolution_clock::time_point t0;
61 float total = 0.f;
62};
63
100class Timer
101{
102public:
108 static Timer &get_instance();
109
115 static void Start(const std::string &name);
116
122 static void Stop(const std::string &name);
123
124 static void Clear();
125
129 static void Dump();
130
131 static std::map<std::string, float> DumpDurations();
132
133 std::map<std::string, Recorder *> get_records() const
134 {
135 return this->records;
136 }
137
138private:
145 Timer(std::string sid = "");
146
150 ~Timer();
151
157 void start(const std::string &name);
158
164 void stop(const std::string &name);
165
169 void dump();
170
171 std::map<std::string, float> dump_durations() const;
172
173 // Deleting the copy constructor and assignment operator to enforce singleton
174 // pattern.
175 Timer(const Timer &) = delete;
176 Timer &operator=(const Timer &) = delete;
177
178private:
179 std::string sid;
180 std::map<std::string, Recorder *> records;
182 std::list<Recorder> data;
183 int current_level = 0;
184};
185
187{
188 ScopedTimer(const char *id) : id(id)
189 {
190 Timer::Start(id);
191 }
193 {
194 Timer::Stop(id);
195 }
196 const char *id;
197};
198
199} // namespace hmap
The Timer class is a singleton that manages multiple Recorders and provides an interface for timing e...
Definition timer.hpp:101
static void Clear()
Definition timer.cpp:64
static Timer & get_instance()
Gets the singleton instance of the Timer class.
Definition timer.cpp:47
static void Dump()
Dumps the timing information for all recorded events to the console.
Definition timer.cpp:69
static void Stop(const std::string &name)
Stops the timer for the specified event name.
Definition timer.cpp:59
static std::map< std::string, float > DumpDurations()
Definition timer.cpp:74
std::map< std::string, Recorder * > get_records() const
Definition timer.hpp:133
static void Start(const std::string &name)
Starts a timer for the specified event name.
Definition timer.cpp:54
Definition opencv.hpp:20
std::chrono::high_resolution_clock::time_point t0
The start time of the event.
Definition timer.hpp:60
std::string name
The name of the event.
Definition timer.hpp:58
Recorder(std::string name)
Constructs a new Recorder object.
Definition timer.cpp:18
void stop()
Stops the timer for this Recorder instance and updates the total elapsed time.
Definition timer.cpp:36
void dump()
Outputs the timing data to the console.
Definition timer.cpp:22
int nb_calls
The number of times the event has been recorded.
Definition timer.hpp:59
void start()
Starts the timer for this Recorder instance.
Definition timer.cpp:30
float total
The total time recorded for the event.
Definition timer.hpp:61
~ScopedTimer()
Definition timer.hpp:192
const char * id
Definition timer.hpp:196
ScopedTimer(const char *id)
Definition timer.hpp:188