12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- //
- // Created by zx on 22-11-3.
- //
- #ifndef SRC_LIO_LIVOX_SRC_UTILS_TIMERECORD_H_
- #define SRC_LIO_LIVOX_SRC_UTILS_TIMERECORD_H_
- #include <chrono>
- #include <fstream>
- #include <map>
- #include <numeric>
- #include <string>
- #include <vector>
- class TimeRecord
- {
- public:
- struct Record {
- Record() = default;
- Record(const std::string& name, double time_usage) {
- func_name_ = name;
- time_usage_in_ms_.emplace_back(time_usage);
- }
- std::string func_name_;
- std::vector<double> time_usage_in_ms_;
- };
- template <class F>
- inline static void Evaluate(F&& func, const std::string& func_name)
- {
- auto t1 = std::chrono::high_resolution_clock::now();
- std::forward<F>(func)();
- auto t2 = std::chrono::high_resolution_clock::now();
- auto time_used = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1).count() * 1000;
- if (records_.find(func_name) != records_.end()) {
- records_[func_name].time_usage_in_ms_.emplace_back(time_used);
- } else {
- records_.insert({func_name, Record(func_name, time_used)});
- }
- }
- static void PrintAll();
- static void DumpIntoFile(const std::string& file_name);
- static double GetMeanTime(const std::string& func_name);
- private:
- static std::map<std::string, Record> records_;
- };
- #endif //SRC_LIO_LIVOX_SRC_UTILS_TIMERECORD_H_
|