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
4#include <chrono>
5#include <string>
6#include <unordered_map>
7#include <vector>
8
9#include <SDL.h>
10
11namespace yaze {
12namespace gfx {
13
45 public:
46 static PerformanceProfiler& Get();
47
54 static void SetEnabled(bool enabled) {
55 Get().enabled_ = enabled;
56 }
57
61 static bool IsEnabled() {
62 return Get().enabled_;
63 }
64
69 static bool IsValid() {
70 return !Get().is_shutting_down_;
71 }
72
78 void StartTimer(const std::string& operation_name);
79
85 void EndTimer(const std::string& operation_name);
86
92 struct TimingStats {
93 double min_time_us = 0.0;
94 double max_time_us = 0.0;
95 double avg_time_us = 0.0;
96 double median_time_us = 0.0;
97 double total_time_ms = 0.0;
98 size_t sample_count = 0;
99 };
100
101 TimingStats GetStats(const std::string& operation_name) const;
102
108 std::string GenerateReport(bool log_to_sdl = true) const;
109
113 void Clear();
114
119 void ClearOperation(const std::string& operation_name);
120
125 std::vector<std::string> GetOperationNames() const;
126
132 bool IsTiming(const std::string& operation_name) const;
133
139 double GetAverageTime(const std::string& operation_name) const;
140
146 double GetTotalTime(const std::string& operation_name) const;
147
153 int GetOperationCount(const std::string& operation_name) const;
154
158 void PrintSummary() const;
159
160 private:
162
163 using TimePoint = std::chrono::high_resolution_clock::time_point;
164 using Duration = std::chrono::microseconds;
165
166 std::unordered_map<std::string, TimePoint> active_timers_;
167 std::unordered_map<std::string, std::vector<double>> operation_times_;
168 std::unordered_map<std::string, double> operation_totals_; // Total time per operation
169 std::unordered_map<std::string, int> operation_counts_; // Count per operation
170
171 bool enabled_ = true; // Performance monitoring enabled by default
172 bool is_shutting_down_ = false; // Flag to prevent operations during destruction
173
179 static double CalculateMedian(std::vector<double> values);
180};
181
192 public:
193 explicit ScopedTimer(const std::string& operation_name);
194 ~ScopedTimer();
195
196 // Disable copy and move
197 ScopedTimer(const ScopedTimer&) = delete;
201
202 private:
203 std::string operation_name_;
204};
205
206} // namespace gfx
207} // namespace yaze
208
209#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 & operator=(const ScopedTimer &)=delete
ScopedTimer(ScopedTimer &&)=delete
ScopedTimer & operator=(ScopedTimer &&)=delete
ScopedTimer(const ScopedTimer &)=delete
Main namespace for the application.
Get timing statistics for an operation.