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
25#include "macrologger.h"
26
27namespace hmap
28{
29
35{
36public:
42 Recorder(std::string name);
43
47 void dump();
48
52 void start();
53
58 void stop();
59
60 std::string name;
61 int nb_calls = 0;
62 std::chrono::high_resolution_clock::time_point
64 float total = 0.f;
65};
66
103class Timer
104{
105public:
111 static Timer &get_instance();
112
118 static void Start(const std::string &name);
119
125 static void Stop(const std::string &name);
126
127 static void Clear();
128
132 static void Dump();
133
134 static std::map<std::string, float> DumpDurations();
135
136 std::map<std::string, Recorder *> get_records() const
137 {
138 return this->records;
139 }
140
141private:
148 Timer(std::string sid = "");
149
153 ~Timer();
154
160 void start(const std::string &name);
161
167 void stop(const std::string &name);
168
172 void dump();
173
174 std::map<std::string, float> dump_durations() const;
175
176 // Deleting the copy constructor and assignment operator to enforce singleton
177 // pattern.
178 Timer(const Timer &) = delete;
179 Timer &operator=(const Timer &) = delete;
180
181private:
182 std::string sid;
183 std::map<std::string, Recorder *>
184 records;
186 std::list<Recorder>
187 data;
188 int current_level = 0;
189};
190
192{
193 ScopedTimer(const char *id)
194 {
195 Timer::Start(id);
196 }
198 {
199 Timer::Stop(id);
200 }
201 const char *id;
202};
203
204} // namespace hmap
The Timer class is a singleton that manages multiple Recorders and provides an interface for timing e...
Definition timer.hpp:104
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:136
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:35
std::chrono::high_resolution_clock::time_point t0
The start time of the event.
Definition timer.hpp:63
std::string name
The name of the event.
Definition timer.hpp:60
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:61
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:64
Definition timer.hpp:192
~ScopedTimer()
Definition timer.hpp:197
const char * id
Definition timer.hpp:201
ScopedTimer(const char *id)
Definition timer.hpp:193