21 : enabled_(true), is_shutting_down_(false) {
37 active_timers_[operation_name] = std::chrono::high_resolution_clock::now();
50 auto end_time = std::chrono::high_resolution_clock::now();
51 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
52 end_time - timer_iter->second)
55 double duration_ms = duration / 1000.0;
66 const std::string& operation_name)
const {
76 const auto& times = times_iter->second;
86 stats.
min_time_us = *std::min_element(times.begin(), times.end());
87 stats.
max_time_us = *std::max_element(times.begin(), times.end());
89 std::accumulate(times.begin(), times.end(), 0.0) / times.size();
92 std::vector<double> sorted_times = times;
93 std::sort(sorted_times.begin(), sorted_times.end());
100 std::ostringstream report;
101 report <<
"\n=== YAZE Unified Performance Report ===\n";
103 report <<
"Performance Monitoring: " << (
enabled_ ?
"ENABLED" :
"DISABLED")
108 report <<
"Memory Pool Usage: " << std::fixed << std::setprecision(2)
109 << (used_bytes / (1024.0 * 1024.0)) <<
" MB / "
110 << (total_bytes / (1024.0 * 1024.0)) <<
" MB\n\n";
117 report <<
"Operation: " << operation <<
"\n";
118 report <<
" Samples: " << stats.sample_count <<
"\n";
119 report <<
" Min: " << std::fixed << std::setprecision(2)
120 << stats.min_time_us <<
" μs\n";
121 report <<
" Max: " << std::fixed << std::setprecision(2)
122 << stats.max_time_us <<
" μs\n";
123 report <<
" Average: " << std::fixed << std::setprecision(2)
124 << stats.avg_time_us <<
" μs\n";
125 report <<
" Median: " << std::fixed << std::setprecision(2)
126 << stats.median_time_us <<
" μs\n";
127 report <<
" Total: " << std::fixed << std::setprecision(2)
128 << stats.total_time_ms <<
" ms\n";
131 if (operation.find(
"palette_lookup") != std::string::npos) {
132 if (stats.avg_time_us < 1.0) {
133 report <<
" Status: ✓ OPTIMIZED (O(1) hash map lookup)\n";
135 report <<
" Status: ⚠ NEEDS OPTIMIZATION (O(n) linear search)\n";
137 }
else if (operation.find(
"texture_update") != std::string::npos) {
138 if (stats.avg_time_us < 100.0) {
139 report <<
" Status: ✓ OPTIMIZED (dirty region tracking)\n";
141 report <<
" Status: ⚠ NEEDS OPTIMIZATION (full texture updates)\n";
143 }
else if (operation.find(
"tile_cache") != std::string::npos) {
144 if (stats.avg_time_us < 10.0) {
145 report <<
" Status: ✓ OPTIMIZED (LRU cache hit)\n";
147 report <<
" Status: ⚠ CACHE MISS (tile recreation needed)\n";
149 }
else if (operation.find(
"::Load") != std::string::npos) {
150 double avg_time_ms = stats.avg_time_us / 1000.0;
151 if (avg_time_ms < 100.0) {
152 report <<
" Status: ✓ FAST LOADING (< 100ms)\n";
153 }
else if (avg_time_ms < 1000.0) {
154 report <<
" Status: ⚠ MODERATE LOADING (100-1000ms)\n";
156 report <<
" Status: ⚠ SLOW LOADING (> 1000ms)\n";
164 report <<
"=== Performance Summary ===\n";
165 size_t total_samples = 0;
166 double total_time = 0.0;
169 total_samples += times.size();
170 total_time += std::accumulate(times.begin(), times.end(), 0.0);
173 if (total_samples > 0) {
174 report <<
"Total Samples: " << total_samples <<
"\n";
175 report <<
"Total Time: " << std::fixed << std::setprecision(2)
176 << total_time / 1000.0 <<
" ms\n";
177 report <<
"Average Time per Operation: " << std::fixed
178 << std::setprecision(2) << total_time / total_samples <<
" μs\n";
181 std::string report_str = report.str();
184 SDL_Log(
"%s", report_str.c_str());
205 std::vector<std::string> names;
208 names.push_back(name);
218 const std::string& operation_name)
const {
227 return total_it->second / count_it->second;
231 const std::string& operation_name)
const {
237 const std::string& operation_name)
const {
243 std::cout <<
"\n=== Performance Summary ===\n";
244 std::cout << std::left << std::setw(30) <<
"Operation" << std::setw(12)
245 <<
"Count" << std::setw(15) <<
"Total (ms)" << std::setw(15)
246 <<
"Average (ms)" <<
"\n";
247 std::cout << std::string(72,
'-') <<
"\n";
258 double total_time = total_it->second;
259 int count = count_it->second;
260 double avg_time = (count > 0) ? total_time / count : 0.0;
262 std::cout << std::left << std::setw(30) << operation_name << std::setw(12)
263 << count << std::setw(15) << std::fixed << std::setprecision(2)
264 << total_time << std::setw(15) << std::fixed
265 << std::setprecision(2) << avg_time <<
"\n";
268 std::cout << std::string(72,
'-') <<
"\n";
272 if (values.empty()) {
276 size_t size = values.size();
278 return (values[size / 2 - 1] + values[size / 2]) / 2.0;
280 return values[size / 2];
285 : operation_name_(operation_name) {
std::pair< size_t, size_t > GetMemoryStats() const
Get memory usage statistics.
static MemoryPool & Get()
ScopedTimer(const std::string &operation_name)
std::string operation_name_