37 active_timers_[operation_name] = std::chrono::high_resolution_clock::now();
49 auto end_time = std::chrono::high_resolution_clock::now();
50 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
51 end_time - timer_iter->second).count();
53 double duration_ms = duration / 1000.0;
64 const std::string& operation_name)
const {
74 const auto& times = times_iter->second;
83 stats.
min_time_us = *std::min_element(times.begin(), times.end());
84 stats.
max_time_us = *std::max_element(times.begin(), times.end());
85 stats.
avg_time_us = std::accumulate(times.begin(), times.end(), 0.0) / times.size();
88 std::vector<double> sorted_times = times;
89 std::sort(sorted_times.begin(), sorted_times.end());
96 std::ostringstream report;
97 report <<
"\n=== YAZE Unified Performance Report ===\n";
99 report <<
"Performance Monitoring: " << (
enabled_ ?
"ENABLED" :
"DISABLED") <<
"\n\n";
103 report <<
"Memory Pool Usage: " << std::fixed << std::setprecision(2)
104 << (used_bytes / (1024.0 * 1024.0)) <<
" MB / "
105 << (total_bytes / (1024.0 * 1024.0)) <<
" MB\n\n";
108 if (times.empty())
continue;
111 report <<
"Operation: " << operation <<
"\n";
112 report <<
" Samples: " << stats.sample_count <<
"\n";
113 report <<
" Min: " << std::fixed << std::setprecision(2) << stats.min_time_us <<
" μs\n";
114 report <<
" Max: " << std::fixed << std::setprecision(2) << stats.max_time_us <<
" μs\n";
115 report <<
" Average: " << std::fixed << std::setprecision(2) << stats.avg_time_us <<
" μs\n";
116 report <<
" Median: " << std::fixed << std::setprecision(2) << stats.median_time_us <<
" μs\n";
117 report <<
" Total: " << std::fixed << std::setprecision(2) << stats.total_time_ms <<
" ms\n";
120 if (operation.find(
"palette_lookup") != std::string::npos) {
121 if (stats.avg_time_us < 1.0) {
122 report <<
" Status: ✓ OPTIMIZED (O(1) hash map lookup)\n";
124 report <<
" Status: ⚠ NEEDS OPTIMIZATION (O(n) linear search)\n";
126 }
else if (operation.find(
"texture_update") != std::string::npos) {
127 if (stats.avg_time_us < 100.0) {
128 report <<
" Status: ✓ OPTIMIZED (dirty region tracking)\n";
130 report <<
" Status: ⚠ NEEDS OPTIMIZATION (full texture updates)\n";
132 }
else if (operation.find(
"tile_cache") != std::string::npos) {
133 if (stats.avg_time_us < 10.0) {
134 report <<
" Status: ✓ OPTIMIZED (LRU cache hit)\n";
136 report <<
" Status: ⚠ CACHE MISS (tile recreation needed)\n";
138 }
else if (operation.find(
"::Load") != std::string::npos) {
139 double avg_time_ms = stats.avg_time_us / 1000.0;
140 if (avg_time_ms < 100.0) {
141 report <<
" Status: ✓ FAST LOADING (< 100ms)\n";
142 }
else if (avg_time_ms < 1000.0) {
143 report <<
" Status: ⚠ MODERATE LOADING (100-1000ms)\n";
145 report <<
" Status: ⚠ SLOW LOADING (> 1000ms)\n";
153 report <<
"=== Performance Summary ===\n";
154 size_t total_samples = 0;
155 double total_time = 0.0;
158 total_samples += times.size();
159 total_time += std::accumulate(times.begin(), times.end(), 0.0);
162 if (total_samples > 0) {
163 report <<
"Total Samples: " << total_samples <<
"\n";
164 report <<
"Total Time: " << std::fixed << std::setprecision(2)
165 << total_time / 1000.0 <<
" ms\n";
166 report <<
"Average Time per Operation: " << std::fixed << std::setprecision(2)
167 << total_time / total_samples <<
" μs\n";
170 std::string report_str = report.str();
173 SDL_Log(
"%s", report_str.c_str());
194 std::vector<std::string> names;
197 names.push_back(name);
211 count_it->second == 0) {
215 return total_it->second / count_it->second;
229 std::cout <<
"\n=== Performance Summary ===\n";
230 std::cout << std::left << std::setw(30) <<
"Operation"
231 << std::setw(12) <<
"Count"
232 << std::setw(15) <<
"Total (ms)"
233 << std::setw(15) <<
"Average (ms)" <<
"\n";
234 std::cout << std::string(72,
'-') <<
"\n";
237 if (times.empty())
continue;
243 double total_time = total_it->second;
244 int count = count_it->second;
245 double avg_time = (count > 0) ? total_time / count : 0.0;
247 std::cout << std::left << std::setw(30) << operation_name
248 << std::setw(12) << count
249 << std::setw(15) << std::fixed << std::setprecision(2) << total_time
250 << std::setw(15) << std::fixed << std::setprecision(2) << avg_time
254 std::cout << std::string(72,
'-') <<
"\n";
258 if (values.empty()) {
262 size_t size = values.size();
264 return (values[size / 2 - 1] + values[size / 2]) / 2.0;
266 return values[size / 2];
271 : 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_
Main namespace for the application.