12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- //
- // Created by zx on 22-11-3.
- //
- #include "TimeRecord.h"
- #include <iostream>
- std::map<std::string, TimeRecord::Record> TimeRecord::records_=std::map<std::string, TimeRecord::Record>();
- void TimeRecord::PrintAll()
- {
- std::cout <<">> ===== Printing run time ====="<<std::endl;
- for (const auto& r : records_) {
- std::cout<< "> [ " << r.first << " ] average time usage: "
- << std::accumulate(r.second.time_usage_in_ms_.begin(), r.second.time_usage_in_ms_.end(), 0.0) /
- double(r.second.time_usage_in_ms_.size())
- << " ms , called times: " << r.second.time_usage_in_ms_.size()<<std::endl;
- }
- std::cout << ">>> ===== Printing run time end ====="<<std::endl;
- }
- void TimeRecord::DumpIntoFile(const std::string& file_name)
- {
- std::ofstream ofs(file_name, std::ios::out);
- if (!ofs.is_open()) {
- std::cout << "Failed to open file: " << file_name<<std::endl;
- return;
- } else {
- std::cout << "Dump Time Records into file: " << file_name<<std::endl;
- }
- size_t max_length = 0;
- for (const auto& iter : records_) {
- ofs << iter.first << ", ";
- if (iter.second.time_usage_in_ms_.size() > max_length) {
- max_length = iter.second.time_usage_in_ms_.size();
- }
- }
- ofs << std::endl;
- for (size_t i = 0; i < max_length; ++i) {
- for (const auto& iter : records_) {
- if (i < iter.second.time_usage_in_ms_.size()) {
- ofs << iter.second.time_usage_in_ms_[i] << ",";
- } else {
- ofs << ",";
- }
- }
- ofs << std::endl;
- }
- ofs.close();
- }
- double TimeRecord::GetMeanTime(const std::string& func_name)
- {
- if (records_.find(func_name) == records_.end()) {
- return 0.0;
- }
- auto r = records_[func_name];
- return std::accumulate(r.time_usage_in_ms_.begin(), r.time_usage_in_ms_.end(), 0.0) /
- double(r.time_usage_in_ms_.size());
- }
|