49 #include "ProfileHackPack_profile_hack.hpp"
56 TimingEntry() : num_calls(0), total_time_secs(0) {}
58 double total_time_secs;
62 typedef std::map<std::string,TimingEntry> func_timing_map_t;
65 func_timing_map_t func_timing_map;
68 class SortByTimingDescending {
70 bool operator()(
const func_timing_map_t::value_type& x
71 ,
const func_timing_map_t::value_type& y )
const
73 return x.second.total_time_secs > y.second.total_time_secs;
80 T my_max(
const T& v1,
const T& v2 ) {
return v1 > v2 ? v1 : v2; }
86 TimingEntry &entry = func_timing_map[func_name];
88 entry.total_time_secs += time_secs;
98 typedef std::vector<std::pair<std::string,TimingEntry> > list_sorted_t;
99 list_sorted_t list_sorted(func_timing_map.size());
101 func_timing_map_t::const_iterator itr_from = func_timing_map.begin();
102 list_sorted_t::iterator itr_to = list_sorted.begin();
103 for( ; itr_from != func_timing_map.end(); ++itr_from, ++itr_to ) {
104 itr_to->first = itr_from->first;
105 itr_to->second = itr_from->second;
108 std::copy( func_timing_map.begin(), func_timing_map.end(), list_sorted.begin() );
109 std::sort( list_sorted.begin(), list_sorted.end(), SortByTimingDescending() );
111 int max_func_name_len = 25;
112 {
for( list_sorted_t::const_iterator itr = list_sorted.begin(); itr != list_sorted.end(); ++itr )
113 max_func_name_len = my_max(
int(max_func_name_len), int(itr->first.size()) );}
116 name_w = max_func_name_len+2,
120 name_ul[] =
"-------------------------",
121 dbl_ul[] =
"--------------------",
122 int_ul[] =
"--------";
124 out <<
"\nPoor man\'s profile times:\n\n";
125 out << left << setw(name_w) <<
"function name"
126 << right << setw(dbl_w) <<
"self+childern(sec)"
127 << right << setw(int_w) <<
"# calls"
128 << right << setw(dbl_w) <<
"av cpu/call(sec)"
130 << left << setw(name_w) << name_ul
131 << right << setw(dbl_w) << dbl_ul
132 << right << setw(int_w) << int_ul
133 << right << setw(dbl_w) << dbl_ul
135 {
for( list_sorted_t::const_iterator itr = list_sorted.begin(); itr != list_sorted.end(); ++itr ) {
136 out << left << setw(name_w) << itr->first
137 << right << setw(dbl_w) << itr->second.total_time_secs
138 << right << setw(int_w) << itr->second.num_calls
139 << right << setw(dbl_w) << (itr->second.total_time_secs / itr->second.num_calls)
void print_timings(std::ostream &out)
Print out the timing generated by calls to set_time().
void set_time(const char func_name[], double time_secs)
Set the name and time for a function or other section of code.