basic version of profiler class
This commit is contained in:
44
ml_profiler.cpp
Normal file
44
ml_profiler.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
#include "ml_profiler.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
void MlPerfMon::turnOn() {
|
||||
perfOn = true;
|
||||
|
||||
}
|
||||
|
||||
void MlPerfMon::add_method_call(const std::string &method) {
|
||||
if (perfOn) {
|
||||
auto search = calls_counter.find(method);
|
||||
if (search != calls_counter.end()) {
|
||||
calls_counter[method] = search->second + 1;
|
||||
} else {
|
||||
calls_counter.insert({method, 1});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MlPerfMon::print_results() {
|
||||
if (perfOn) {
|
||||
std::cerr << std::endl << std::endl << "Call Frequency" << std::endl;
|
||||
|
||||
using callcount = std::pair<std::string, long>;
|
||||
|
||||
std::vector<callcount> v(begin(calls_counter), end(calls_counter));
|
||||
std::sort(std::begin(v), std::end(v), [](const callcount& a, const callcount& b) { return a.second > b.second; });
|
||||
|
||||
int i {0};
|
||||
for(auto &p : v) {
|
||||
// TODO when method name longer than 15 chars, here it crashes
|
||||
p.first.insert(p.first.end(), 15 - p.first.size(), ' ');
|
||||
std::cerr << p.first << " " << p.second << std::endl;
|
||||
|
||||
i++;
|
||||
if (i > 10) break;
|
||||
}
|
||||
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user