Skip to content

Instantly share code, notes, and snippets.

@Rainyan
Last active October 30, 2022 17:50
Show Gist options
  • Save Rainyan/b2dd093eec2dca304346837d68a19132 to your computer and use it in GitHub Desktop.
Save Rainyan/b2dd093eec2dca304346837d68a19132 to your computer and use it in GitHub Desktop.
Quick and dirty SourcePawn inline benchmark for when you're feeling lazy. Just #include <benchmark> & wrap the tested code between BENCHMARK_START(); ... BENCHMARK_END(); Note that the START-END "stack" has to be unwound in the same LIFO order.
#if defined(_BENCHMARK_INC_H_)
#endinput
#else
#define _BENCHMARK_INC_H_
#endif
#include <profiler>
// Can nest max this many profilers, total.
#define MAX_PROFILERS 32
#define MAX_PROFILE_SECTIONNAME_STRLEN 32
static Profiler _profilers_stack[MAX_PROFILERS];
static int _profilers_index = 0;
static char _profilers_sections[MAX_PROFILERS][MAX_PROFILE_SECTIONNAME_STRLEN];
stock void BENCHMARK_START(const char[] section="unnamed")
{
PrintToServer("BENCHMARK START: %s", section);
_profilers_stack[_profilers_index] = new Profiler();
strcopy(_profilers_sections[_profilers_index], sizeof(_profilers_sections[]), section);
_profilers_stack[_profilers_index++].Start();
}
stock void BENCHMARK_END()
{
_profilers_stack[--_profilers_index].Stop();
PrintToServer("%s FINISHED IN TIME: %f", _profilers_sections[_profilers_index], _profilers_stack[_profilers_index].Time);
delete _profilers_stack[_profilers_index];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment