TimeRecord.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // Created by zx on 22-11-3.
  3. //
  4. #ifndef SRC_LIO_LIVOX_SRC_UTILS_TIMERECORD_H_
  5. #define SRC_LIO_LIVOX_SRC_UTILS_TIMERECORD_H_
  6. #include <chrono>
  7. #include <fstream>
  8. #include <map>
  9. #include <numeric>
  10. #include <string>
  11. #include <vector>
  12. class TimeRecord
  13. {
  14. public:
  15. struct Record {
  16. Record() = default;
  17. Record(const std::string& name, double time_usage) {
  18. func_name_ = name;
  19. time_usage_in_ms_.emplace_back(time_usage);
  20. }
  21. std::string func_name_;
  22. std::vector<double> time_usage_in_ms_;
  23. };
  24. template <class F>
  25. inline static void Evaluate(F&& func, const std::string& func_name)
  26. {
  27. auto t1 = std::chrono::high_resolution_clock::now();
  28. std::forward<F>(func)();
  29. auto t2 = std::chrono::high_resolution_clock::now();
  30. auto time_used = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1).count() * 1000;
  31. if (records_.find(func_name) != records_.end()) {
  32. records_[func_name].time_usage_in_ms_.emplace_back(time_used);
  33. } else {
  34. records_.insert({func_name, Record(func_name, time_used)});
  35. }
  36. }
  37. static void PrintAll();
  38. static void DumpIntoFile(const std::string& file_name);
  39. static double GetMeanTime(const std::string& func_name);
  40. private:
  41. static std::map<std::string, Record> records_;
  42. };
  43. #endif //SRC_LIO_LIVOX_SRC_UTILS_TIMERECORD_H_