Files
karl/include/tools/simulation_time.h
T
2024-12-04 17:15:04 +01:00

36 lines
1.3 KiB
C++
Executable File

#ifndef SIMULATION_TIME_H
#define SIMULATION_TIME_H
#include <chrono>
#include <map>
#include <string>
// structure to measure a timing (stores begin and endpoint and the corresponding timespan)
struct TimerEvent {
std::chrono::time_point<std::chrono::high_resolution_clock> start;
std::chrono::time_point<std::chrono::high_resolution_clock> stop;
std::chrono::duration<double, std::nano> total_timespan;
};
// class to time between a start and endpoint if the code has been compiled with
// USE_TIMER = true, otherwise does nothing
class SimulationTimer {
public:
// if first invocation of eventStartPoint with a specific event_name, adds a
// TimerEvent at the time it was called; otherwise sets the value to
// the time it has been invoked
static void eventStartPoint(std::string event_name);
// sets the endpoint in the TimerEvent struct to the time the function was called
// the event_name has to match with an eventStartPoint invocation (not checked)
// calculates the timedifference
static void eventStopPoint(std::string event_name);
// print a formatted string containing all event_names and their respective timing to stdout
static void printDurations();
private:
// map to store all event_names with their TimerEvent structs
static std::map<std::string, TimerEvent> timed_events_;
};
#endif /* SIMULATION_TIME_H */