Skip to content

Instantly share code, notes, and snippets.

@singalen
Last active June 20, 2017 20:12
Show Gist options
  • Save singalen/07bf1ea818e7e07c167c124b77f02502 to your computer and use it in GitHub Desktop.
Save singalen/07bf1ea818e7e07c167c124b77f02502 to your computer and use it in GitHub Desktop.
static ProfileCounter counter_one("profiled_function");
void profiled_function()
{
...
{
ProfileGuard g(&counter_one);
do_some_timed_activity();
}
...
}
class ProfileCounter
{
string name;
long calls;
clock_t total_time;
clock_t last_call;
ProfileCounter(string aname): name(aname), calls(0), total_time(0), last_call(0) {}
~ProfileCounter()
{
if (calls > 0) {
std::cout << "Profiler " << name << ": " << " no calls\n";
} else {
std::cout << "Profiler " << name << ": " << calls << " calls, " << ((double) total_time) / calls / CLOCKS_PER_SEC << "s average\n";
}
}
}
class ProfileGuard
{
ProfileCounter *counter;
ProfileGuard(ProfileCounter *a_counter): counter(a_counter)
{
counter->calls++;
counter->last_call = clock();
}
~ProfileGuard()
{
counter->total_time += clock() - last_call;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment