yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
performance_profiler.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_PERFORMANCE_PERFORMANCE_PROFILER_H
2#define YAZE_APP_GFX_PERFORMANCE_PERFORMANCE_PROFILER_H
3
5
6#include <chrono>
7#include <string>
8#include <unordered_map>
9#include <vector>
10
11namespace yaze {
12namespace gfx {
13
45 public:
46 static PerformanceProfiler& Get();
47
54 static void SetEnabled(bool enabled) { Get().enabled_ = enabled; }
55
59 static bool IsEnabled() { return Get().enabled_; }
60
65 static bool IsValid() { return !Get().is_shutting_down_; }
66
72 void StartTimer(const std::string& operation_name);
73
79 void EndTimer(const std::string& operation_name);
80
86 struct TimingStats {
87 double min_time_us = 0.0;
88 double max_time_us = 0.0;
89 double avg_time_us = 0.0;
90 double median_time_us = 0.0;
91 double total_time_ms = 0.0;
92 size_t sample_count = 0;
93 };
94
95 TimingStats GetStats(const std::string& operation_name) const;
96
102 std::string GenerateReport(bool log_to_sdl = true) const;
103
107 void Clear();
108
113 void ClearOperation(const std::string& operation_name);
114
119 std::vector<std::string> GetOperationNames() const;
120
126 bool IsTiming(const std::string& operation_name) const;
127
133 double GetAverageTime(const std::string& operation_name) const;
134
140 double GetTotalTime(const std::string& operation_name) const;
141
147 int GetOperationCount(const std::string& operation_name) const;
148
152 void PrintSummary() const;
153
154 private:
156
157 using TimePoint = std::chrono::high_resolution_clock::time_point;
158 using Duration = std::chrono::microseconds;
159
160 std::unordered_map<std::string, TimePoint> active_timers_;
161 std::unordered_map<std::string, std::vector<double>> operation_times_;
162 std::unordered_map<std::string, double>
163 operation_totals_; // Total time per operation
164 std::unordered_map<std::string, int>
165 operation_counts_; // Count per operation
166
167 bool enabled_ = true; // Performance monitoring enabled by default
169 false; // Flag to prevent operations during destruction
170
176 static double CalculateMedian(std::vector<double> values);
177};
178
189 public:
190 explicit ScopedTimer(const std::string& operation_name);
191 ~ScopedTimer();
192
193 // Disable copy and move
194 ScopedTimer(const ScopedTimer&) = delete;
198
199 private:
200 std::string operation_name_;
201};
202
203} // namespace gfx
204} // namespace yaze
205
206#endif // YAZE_APP_GFX_PERFORMANCE_PERFORMANCE_PROFILER_H
Unified performance profiler for all YAZE operations.
void Clear()
Clear all timing data.
void StartTimer(const std::string &operation_name)
Start timing an operation.
double GetAverageTime(const std::string &operation_name) const
Get the average time for an operation in milliseconds.
static void SetEnabled(bool enabled)
Enable or disable performance monitoring.
std::vector< std::string > GetOperationNames() const
Get list of all tracked operations.
TimingStats GetStats(const std::string &operation_name) const
void ClearOperation(const std::string &operation_name)
Clear timing data for a specific operation.
static PerformanceProfiler & Get()
int GetOperationCount(const std::string &operation_name) const
Get the number of times an operation was measured.
static double CalculateMedian(std::vector< double > values)
Calculate median value from a sorted vector.
std::string GenerateReport(bool log_to_sdl=true) const
Generate a comprehensive performance report.
void EndTimer(const std::string &operation_name)
End timing an operation.
std::unordered_map< std::string, int > operation_counts_
std::unordered_map< std::string, TimePoint > active_timers_
std::chrono::microseconds Duration
std::chrono::high_resolution_clock::time_point TimePoint
static bool IsValid()
Check if the profiler is in a valid state (not shutting down) This prevents crashes during static des...
static bool IsEnabled()
Check if performance monitoring is enabled.
std::unordered_map< std::string, double > operation_totals_
std::unordered_map< std::string, std::vector< double > > operation_times_
double GetTotalTime(const std::string &operation_name) const
Get the total time for an operation in milliseconds.
void PrintSummary() const
Print a summary of all operations to console.
bool IsTiming(const std::string &operation_name) const
Check if an operation is currently being timed.
RAII timer for automatic timing management.
ScopedTimer(const std::string &operation_name)
ScopedTimer & operator=(const ScopedTimer &)=delete
ScopedTimer(ScopedTimer &&)=delete
ScopedTimer & operator=(ScopedTimer &&)=delete
ScopedTimer(const ScopedTimer &)=delete
SDL2/SDL3 compatibility layer.
Get timing statistics for an operation.