twn_util.c: make profiler collect worst case, dispaly the stats with awareness of sample count

This commit is contained in:
veclavtalica 2025-01-15 04:55:18 +03:00
parent 1ba33bdc26
commit 8c165974c7

View File

@ -15,6 +15,7 @@ static struct ProfileItem {
uint64_t tick_start; uint64_t tick_start;
uint64_t tick_accum; uint64_t tick_accum;
uint64_t sample_count; uint64_t sample_count;
uint64_t worst_tick;
} value; } value;
} *profiles; } *profiles;
@ -323,17 +324,35 @@ void profile_end(char profile[const static 1]) {
return; return;
} }
uint64_t took = SDL_GetPerformanceCounter() - p->value.tick_start;
p->value.sample_count++; p->value.sample_count++;
p->value.tick_accum += SDL_GetPerformanceCounter() - p->value.tick_start; p->value.tick_accum += took;
if (p->value.worst_tick < took)
p->value.worst_tick = took;
} }
void profile_list_stats(void) { void profile_list_stats(void) {
for (size_t i = 0; i < shlenu(profiles); ++i) { for (size_t i = 0; i < shlenu(profiles); ++i) {
log_info("profile '%s' on average took: %f seconds", if (profiles[i].value.sample_count == 0) {
log_warn("Profile %s was started, but not once finished.", profiles[i].key);
}
else if (profiles[i].value.sample_count == 1) {
log_info("Profile '%s' took: %f seconds",
profiles[i].key,
(double)profiles[i].value.tick_accum / 1 / (double)(SDL_GetPerformanceFrequency()));
}
else if (profiles[i].value.sample_count > 1) {
log_info("Profile '%s' on average took: %f seconds, worst case: %s, sample count: %llu",
profiles[i].key, profiles[i].key,
(double)profiles[i].value.tick_accum / (double)profiles[i].value.tick_accum /
(double)profiles[i].value.sample_count / (double)profiles[i].value.sample_count /
(double)(SDL_GetPerformanceFrequency())); (double)(SDL_GetPerformanceFrequency()),
(double)profiles[i].value.worst_tick / 1 / (double)(SDL_GetPerformanceFrequency()),
profiles[i].value.sample_count);
}
} }
} }