Last active
June 20, 2017 20:12
-
-
Save singalen/07bf1ea818e7e07c167c124b77f02502 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static ProfileCounter counter_one("profiled_function"); | |
void profiled_function() | |
{ | |
... | |
{ | |
ProfileGuard g(&counter_one); | |
do_some_timed_activity(); | |
} | |
... | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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