mlisp/ml_profiler.cpp

45 lines
1.1 KiB
C++

#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;
}
}