diff --git a/src/twn_util.c b/src/twn_util.c index 05db8ad..85a0aff 100644 --- a/src/twn_util.c +++ b/src/twn_util.c @@ -9,6 +9,14 @@ #include +static struct ProfileItem { + char const *key; + struct Profile { + uint64_t tick; + } value; +} *profiles; + + void cry_impl(const char *file, const int line, const char *title, const char *text) { SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "TEARS AT %s:%d: %s ... %s", file, line, title, text); @@ -287,3 +295,19 @@ char *expand_asterisk(const char *mask, const char *to) { SDL_memcpy(str + (offset - mask) + to_len, offset + 1, mask_len - (offset - mask)); return str; } + + +void profile_start(char profile[const static 1]) { + shput(profiles, profile, (struct Profile){ .tick = SDL_GetPerformanceCounter() }); +} + + +void profile_end(char profile[const static 1]) { + struct ProfileItem *p = shgetp_null(profiles, profile); + if (!p) { + 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); +} diff --git a/src/twn_util_c.h b/src/twn_util_c.h index c867ea3..5f83682 100644 --- a/src/twn_util_c.h +++ b/src/twn_util_c.h @@ -23,6 +23,9 @@ _Noreturn void die_abruptly(void); /* note: you must free the returned string */ 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]); + /* http://www.azillionmonkeys.com/qed/sqroot.html */ static inline float fast_sqrt(float x) {