added runtime in ms to -p option

This commit is contained in:
VaclavT 2021-03-28 23:52:33 +02:00
parent c75a04a4f7
commit a92020ac1e
4 changed files with 30 additions and 10 deletions

View File

@ -46,4 +46,4 @@
(print (and (print "xx") (= 1 0) (print "yy"))) (print (and (print "xx") (= 1 0) (print "yy")))
(sleep 1.5) ; (sleep 1.5)

6
ml.cpp
View File

@ -1922,6 +1922,9 @@ int main(int argc, char *argv[]) {
srand(time(NULL)); srand(time(NULL));
try { try {
// performance monitor on
if (cmdOptionExists(argv, argv + argc, "-p"))
MlPerfMon::instance().turnOn();
// skip loading std lib // skip loading std lib
if (!cmdOptionExists(argv, argv + argc, "-b")) { if (!cmdOptionExists(argv, argv + argc, "-b")) {
load_std_lib(env); load_std_lib(env);
@ -1936,9 +1939,6 @@ int main(int argc, char *argv[]) {
std::cout << VERSION << std::endl; std::cout << VERSION << std::endl;
return 0; return 0;
} }
// performance monitor on
if (cmdOptionExists(argv, argv + argc, "-p"))
MlPerfMon::instance().turnOn();
// passed code // passed code
if (cmdOptionExists(argv, argv + argc, "-c")) { if (cmdOptionExists(argv, argv + argc, "-c")) {
std::vector<std::string> codes = getCmdOption(argv, argc, "-c"); std::vector<std::string> codes = getCmdOption(argv, argc, "-c");

View File

@ -3,10 +3,14 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <numeric>
using namespace std::chrono;
void MlPerfMon::turnOn() { void MlPerfMon::turnOn() {
perfOn = true; perfOn = true;
start_time = std::chrono::high_resolution_clock::now();
} }
void MlPerfMon::add_method_call(const std::string &method) { void MlPerfMon::add_method_call(const std::string &method) {
@ -22,21 +26,32 @@ void MlPerfMon::add_method_call(const std::string &method) {
void MlPerfMon::print_results() { void MlPerfMon::print_results() {
if (perfOn) { if (perfOn) {
std::cerr << std::endl << std::endl << "Call Frequency" << std::endl; std::chrono::time_point<std::chrono::high_resolution_clock> end_time = std::chrono::high_resolution_clock::now();
using callcount = std::pair<std::string, long>; using callcount = std::pair<std::string, long>;
std::vector<callcount> v(begin(calls_counter), end(calls_counter)); 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; }); std::sort(std::begin(v), std::end(v), [](const callcount& a, const callcount& b) { return a.second > b.second; });
long total_calls = 0;
for(auto const& c : v) { total_calls += c.second; }
std::cerr << std::endl << std::endl;
std::cerr << "perf stats" << std::endl;
std::cerr << "run time : " << duration_cast<milliseconds>(end_time - start_time).count() << " ms " << std::endl;
std::cerr << "total calls : " << total_calls << std::endl;
std::cerr << "methods called : " << calls_counter.size() << std::endl;
std::cerr << std::endl;
int i {0}; int i {0};
for(auto &p : v) { for(auto &p : v) {
// TODO when method name longer than 15 chars, here it crashes if (p.first.size() >= method_name_print_len)
p.first.insert(p.first.end(), 15 - p.first.size(), ' '); p.first.erase(method_name_print_len, std::string::npos);
else
p.first.insert(p.first.end(), method_name_print_len - p.first.size(), ' ');
std::cerr << p.first << " " << p.second << std::endl; std::cerr << p.first << " " << p.second << std::endl;
i++; i++;
if (i > 10) break; if (i > print_top_methods) break;
} }
std::cerr << std::endl; std::cerr << std::endl;

View File

@ -3,6 +3,10 @@
#include "ml.h" #include "ml.h"
#include <unordered_map> #include <unordered_map>
#include <chrono>
static const int method_name_print_len = 15;
static const int print_top_methods = 15;
class MlPerfMon { class MlPerfMon {
@ -25,4 +29,5 @@ public:
private: private:
bool perfOn; bool perfOn;
std::unordered_map<std::string, long> calls_counter; std::unordered_map<std::string, long> calls_counter;
std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
}; };