From 8c165974c7ae32fc646f456dade1eea5096fbbfc Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Wed, 15 Jan 2025 04:55:18 +0300 Subject: [PATCH] twn_util.c: make profiler collect worst case, dispaly the stats with awareness of sample count --- src/twn_util.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/twn_util.c b/src/twn_util.c index e022924..6874582 100644 --- a/src/twn_util.c +++ b/src/twn_util.c @@ -15,6 +15,7 @@ static struct ProfileItem { uint64_t tick_start; uint64_t tick_accum; uint64_t sample_count; + uint64_t worst_tick; } value; } *profiles; @@ -323,17 +324,35 @@ void profile_end(char profile[const static 1]) { return; } + uint64_t took = SDL_GetPerformanceCounter() - p->value.tick_start; + 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) { for (size_t i = 0; i < shlenu(profiles); ++i) { - log_info("profile '%s' on average took: %f seconds", - profiles[i].key, - (double)profiles[i].value.tick_accum / - (double)profiles[i].value.sample_count / - (double)(SDL_GetPerformanceFrequency())); + 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, + (double)profiles[i].value.tick_accum / + (double)profiles[i].value.sample_count / + (double)(SDL_GetPerformanceFrequency()), + (double)profiles[i].value.worst_tick / 1 / (double)(SDL_GetPerformanceFrequency()), + profiles[i].value.sample_count); + } } }