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
24#include "macrologger.h"
25
26namespace hmap
27{
28
34{
35public:
41 Recorder(std::string name);
42
46 void dump();
47
51 void start();
52
57 void stop();
58
59 std::string name;
60 int nb_calls = 0;
61 std::chrono::high_resolution_clock::time_point
63 float total = 0.f;
64};
65
102class Timer
103{
104public:
110 static Timer &get_instance();
111
117 static void Start(const std::string &name);
118
124 static void Stop(const std::string &name);
125
126 static void Clear();
127
131 static void Dump();
132
133 static std::map<std::string, float> DumpDurations();
134
135 std::map<std::string, Recorder *> get_records() const
136 {
137 return this->records;
138 }
139
140private:
147 Timer(std::string sid = "");
148
152 ~Timer();
153
159 void start(const std::string &name);
160
166 void stop(const std::string &name);
167
171 void dump();
172
173 std::map<std::string, float> dump_durations() const;
174
175 // Deleting the copy constructor and assignment operator to enforce singleton
176 // pattern.
177 Timer(const Timer &) = delete;
178 Timer &operator=(const Timer &) = delete;
179
180private:
181 std::string sid;
182 std::map<std::string, Recorder *>
183 records;
185 std::list<Recorder>
186 data;
187 int current_level = 0;
188};
189
190} // namespace hmap
The Timer class is a singleton that manages multiple Recorders and provides an interface for timing e...
Definition timer.hpp:103
static void Clear()
Definition timer.cpp:56
static Timer & get_instance()
Gets the singleton instance of the Timer class.
Definition timer.cpp:39
static void Dump()
Dumps the timing information for all recorded events to the console.
Definition timer.cpp:61
static void Stop(const std::string &name)
Stops the timer for the specified event name.
Definition timer.cpp:51
static std::map< std::string, float > DumpDurations()
Definition timer.cpp:66
std::map< std::string, Recorder * > get_records() const
Definition timer.hpp:135
static void Start(const std::string &name)
Starts a timer for the specified event name.
Definition timer.cpp:46
Definition algebra.hpp:23
The Recorder class is responsible for recording timing information for individual events.
Definition timer.hpp:34
std::chrono::high_resolution_clock::time_point t0
The start time of the event.
Definition timer.hpp:62
std::string name
The name of the event.
Definition timer.hpp:59
void stop()
Stops the timer for this Recorder instance and updates the total elapsed time.
Definition timer.cpp:28
void dump()
Outputs the timing data to the console.
Definition timer.cpp:14
int nb_calls
The number of times the event has been recorded.
Definition timer.hpp:60
void start()
Starts the timer for this Recorder instance.
Definition timer.cpp:22
float total
The total time recorded for the event.
Definition timer.hpp:63