voidScopeProfiler::onDestroy(ScopeProfiler::ClockType::time_point end){ auto diff = end - beg; int us = std::chrono::duration_cast<std::chrono::microseconds>(diff).count(); records.push_back({tag, us}); }
voidScopeProfiler::printLog(std::ostream &out){ if (records.size() == 0) { return; }
structStatistic { int max_us = 0; int min_us = 0; int total_us = 0; int count_rec = 0; constchar *tag = nullptr; }; std::map<std::string_view, Statistic> stats; for (autoconst &[tag, us]: records) { auto &stat = stats[tag]; stat.total_us += us; stat.max_us = std::max(stat.max_us, us); stat.min_us = !stat.count_rec ? us : std::min(stat.min_us, us); stat.count_rec++; stat.tag = tag; }
auto dump = [&out] (int val, int w) { auto tpwv = 1; for (int i = 0; i < w - 1; i++) tpwv *= 10; if (val > tpwv) { if (val / 1000 > tpwv / 10) { out << std::setw(w - 1) << val / 1000000 << 'M'; } else { out << std::setw(w - 1) << val / 1000 << 'k'; } } else { out << std::setw(w) << val; } };
out << " avg | min | max | total | cnt | tag\n"; for (autoconst &[tag, stat]: sortstats) { dump(stat.total_us / stat.count_rec, 9); out << '|'; dump(stat.min_us, 9); out << '|'; dump(stat.max_us, 9); out << '|'; dump(stat.total_us, 9); out << '|'; dump(stat.count_rec, 5); out << '|'; out << ' ' << tag << '\n'; } }