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
62 float total = 0.f;
63};
64
101class Timer
102{
103public:
109 static Timer &get_instance();
110
116 static void Start(const std::string &name);
117
123 static void Stop(const std::string &name);
124
125 static void Clear();
126
130 static void Dump();
131
132 static std::map<std::string, float> DumpDurations();
133
134 std::map<std::string, Recorder *> get_records() const
135 {
136 return this->records;
137 }
138
139private:
146 Timer(std::string sid = "");
147
151 ~Timer();
152
158 void start(const std::string &name);
159
165 void stop(const std::string &name);
166
170 void dump();
171
172 std::map<std::string, float> dump_durations() const;
173
174 // Deleting the copy constructor and assignment operator to enforce singleton
175 // pattern.
176 Timer(const Timer &) = delete;
177 Timer &operator=(const Timer &) = delete;
178
179private:
180 std::string sid;
181 std::map<std::string, Recorder *>
182 records;
184 std::list<Recorder>
185 data;
186 int current_level = 0;
187};
188
190{
191 ScopedTimer(const char *id)
192 {
193 Timer::Start(id);
194 }
196 {
197 Timer::Stop(id);
198 }
199 const char *id;
200};
201
202} // namespace hmap
The Timer class is a singleton that manages multiple Recorders and provides an interface for timing e...
Definition timer.hpp:102
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:134
static void Start(const std::string &name)
Starts a timer for the specified event name.
Definition timer.cpp:54
Definition algebra.hpp:23
The Recorder class is responsible for recording timing information for individual events.
Definition timer.hpp:33
std::chrono::high_resolution_clock::time_point t0
The start time of the event.
Definition timer.hpp:61
std::string name
The name of the event.
Definition timer.hpp:58
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:62
Definition timer.hpp:190
~ScopedTimer()
Definition timer.hpp:195
const char * id
Definition timer.hpp:199
ScopedTimer(const char *id)
Definition timer.hpp:191