From 2f94e17852667a12724ad62f3d74914e5754f965 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Mon, 4 Nov 2024 16:22:13 +0300 Subject: [PATCH] twn_util_c.h: profile_list_stats() for average summary, without spam in console --- src/twn_loop.c | 2 ++ src/twn_util.c | 34 ++++++++++++++++++++++++++++++---- src/twn_util_c.h | 1 + 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/twn_loop.c b/src/twn_loop.c index 7461266..7d07ce0 100644 --- a/src/twn_loop.c +++ b/src/twn_loop.c @@ -788,6 +788,8 @@ int enter_loop(int argc, char **argv) { main_loop(); } + profile_list_stats(); + /* loop is over */ game_object_unload(); diff --git a/src/twn_util.c b/src/twn_util.c index 85a0aff..e022924 100644 --- a/src/twn_util.c +++ b/src/twn_util.c @@ -12,7 +12,9 @@ static struct ProfileItem { char const *key; struct Profile { - uint64_t tick; + uint64_t tick_start; + uint64_t tick_accum; + uint64_t sample_count; } value; } *profiles; @@ -298,7 +300,19 @@ char *expand_asterisk(const char *mask, const char *to) { void profile_start(char profile[const static 1]) { - shput(profiles, profile, (struct Profile){ .tick = SDL_GetPerformanceCounter() }); + uint64_t tick_accum = 0, sample_count = 0; + + struct ProfileItem const *p = shgetp_null(profiles, profile); + if (p) { + tick_accum = p->value.tick_accum; + sample_count = p->value.sample_count; + } + + shput(profiles, profile, ((struct Profile) { + .tick_start = SDL_GetPerformanceCounter(), + .tick_accum = tick_accum, + .sample_count = sample_count, + })); } @@ -308,6 +322,18 @@ void profile_end(char profile[const static 1]) { log_warn("profile %s wasn't started!", profile); return; } - log_info("profile '%s' took: %fsec", profile, (double)(SDL_GetPerformanceCounter() - p->value.tick) / (double)(SDL_GetPerformanceFrequency())); - shdel(profiles, profile); + + p->value.sample_count++; + p->value.tick_accum += SDL_GetPerformanceCounter() - p->value.tick_start; +} + + +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())); + } } diff --git a/src/twn_util_c.h b/src/twn_util_c.h index 5f83682..8beb33e 100644 --- a/src/twn_util_c.h +++ b/src/twn_util_c.h @@ -25,6 +25,7 @@ char *expand_asterisk(const char *mask, const char *to); void profile_start(char profile[const static 1]); void profile_end(char profile[const static 1]); +void profile_list_stats(void); /* http://www.azillionmonkeys.com/qed/sqroot.html */ static inline float fast_sqrt(float x)