TimerRecord.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // Created by zx on 22-11-3.
  3. //
  4. #ifndef SRC_LIO_LIVOX_SRC_UTILS_TIMERRECORD_H_
  5. #define SRC_LIO_LIVOX_SRC_UTILS_TIMERRECORD_H_
  6. #include <map>
  7. #include <mutex>
  8. #include <string>
  9. #include <vector>
  10. #include <chrono>
  11. #include <numeric>
  12. #include <iostream>
  13. #include <algorithm>
  14. struct FuncRecord
  15. {
  16. public:
  17. FuncRecord()=default;
  18. FuncRecord(std::string name,double time)
  19. {
  20. name_=name;
  21. timesqueue_.emplace_back(time);
  22. }
  23. std::string name_;
  24. std::vector<double> timesqueue_;
  25. };
  26. class TimerRecord
  27. {
  28. public:
  29. public:
  30. template<class F>
  31. inline static void Execute(F func, const std::string &func_name)
  32. {
  33. auto t1 = std::chrono::high_resolution_clock::now();
  34. func();
  35. auto t2 = std::chrono::high_resolution_clock::now();
  36. auto time_used = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1).count() * 1000;
  37. mutex_.lock();
  38. if (records_.find(func_name) != records_.end())
  39. {
  40. records_[func_name].timesqueue_.emplace_back(time_used);
  41. }
  42. else
  43. {
  44. records_.insert({func_name, FuncRecord(func_name, time_used)});
  45. }
  46. mutex_.unlock();
  47. }
  48. static void PrintAll();
  49. protected:
  50. static std::mutex mutex_;
  51. static std::map<std::string, FuncRecord> records_;
  52. };
  53. #endif //SRC_LIO_LIVOX_SRC_UTILS_TIMERRECORD_H_