nndeploy C++ API  0.2.0
nndeploy C++ API
time_profiler.h
Go to the documentation of this file.
1 
2 #ifndef _NNDEPLOY_BASE_TIME_PROFILER_H_
3 #define _NNDEPLOY_BASE_TIME_PROFILER_H_
4 
5 #include "nndeploy/base/common.h"
7 #include "nndeploy/base/macro.h"
8 #include "nndeploy/base/object.h"
9 
10 namespace nndeploy {
11 namespace base {
12 
14  public:
16  virtual ~TimeProfiler();
17 
18  void reset();
19  void start(const std::string &key);
20  void end(const std::string &key);
21  float getCostTime(const std::string &key) const;
22  float getAverageTime(const std::string &key) const;
23  void print(const std::string &title = "");
24  void printIndex(const std::string &title, uint64_t index);
25  void printRemoveWarmup(const std::string &title, uint64_t warmup_times);
26 
27  private:
28  enum Type {
29  kStart,
30  kEnd,
31  };
32  struct Record {
33  Record(const std::string &key, int64_t order, uint64_t start, int max_size)
34  : key_(key),
35  type_(kStart),
36  order_(order),
37  call_times_(1),
38  cost_time_sum_(0),
39  flops_(0.0f),
40  start_(start) {
41  cost_time_.resize(max_size);
42  }
43  Record(const std::string &key, int64_t order, uint64_t start, float flops,
44  int max_size)
45  : key_(key),
46  type_(kStart),
47  order_(order),
48  call_times_(1),
49  cost_time_sum_(0),
50  flops_(flops),
51  start_(start) {
52  cost_time_.resize(max_size);
53  }
54  std::string key_;
55  Type type_;
56  int order_;
57  int call_times_;
58  std::vector<uint64_t> cost_time_;
59  uint64_t cost_time_sum_;
60  float flops_;
61  uint64_t start_;
62  };
63 
64  private:
65  int64_t order_ = 0;
66  std::map<std::string, std::shared_ptr<Record>> records_;
67  int max_size_ = 1024;
68 };
69 
71 
72 extern NNDEPLOY_CC_API void timePointStart(const std::string &key);
73 
74 extern NNDEPLOY_CC_API void timePointEnd(const std::string &key);
75 
76 extern NNDEPLOY_CC_API float timeProfilerGetCostTime(const std::string &key);
77 
78 extern NNDEPLOY_CC_API float timeProfilerGetAverageTime(const std::string &key);
79 
80 extern NNDEPLOY_CC_API void timeProfilerPrint(const std::string &title = "");
81 
82 extern NNDEPLOY_CC_API void timeProfilerPrintIndex(const std::string &title,
83  uint64_t index);
84 
86  const std::string &title, uint64_t warmup_times);
87 
89  //
90  // AutoTime.hpp
91  // MNN
92  //
93  // Created by MNN on 2018/07/27.
94  // Copyright © 2018, Alibaba Group Holding Limited
95  //
96  public:
97  Timer();
98  ~Timer();
99  Timer(const Timer &) = delete;
100  Timer(const Timer &&) = delete;
101  Timer &operator=(const Timer &) = delete;
102  Timer &operator=(const Timer &&) = delete;
103 
104  // reset timer
105  void reset();
106  // get duration (us) from init or latest reset.
107  uint64_t durationInUs();
108 
109  // Get Current Time
110  uint64_t current() const { return mLastResetTime; }
111 
112  protected:
113  uint64_t mLastResetTime;
114 };
115 
117  //
118  // AutoTime.hpp
119  // MNN
120  //
121  // Created by MNN on 2018/07/27.
122  // Copyright © 2018, Alibaba Group Holding Limited
123  //
124  public:
125  AutoTime(int line, const char *func);
127  AutoTime(const AutoTime &) = delete;
128  AutoTime(const AutoTime &&) = delete;
129  AutoTime &operator=(const AutoTime &) = delete;
130  AutoTime &operator=(const AutoTime &&) = delete;
131 
132  private:
133  int mLine;
134  char *mName;
135  uint64_t mCurrentTime;
136 };
137 
138 } // namespace base
139 } // namespace nndeploy
140 
141 #ifdef ENABLE_NNDEPLOY_TIME_PROFILER
142 #define NNDEPLOY_TIME_PROFILER_RESET() nndeploy::base::timeProfilerReset()
143 #define NNDEPLOY_TIME_POINT_START(key) nndeploy::base::timePointStart(key)
144 #define NNDEPLOY_TIME_POINT_END(key) nndeploy::base::timePointEnd(key)
145 #define NNDEPLOY_TIME_PROFILER_GET_COST_TIME(key) \
146  nndeploy::base::timeProfilerGetCostTime(key)
147 #define NNDEPLOY_TIME_PROFILER_GET_AVERAGE_TIME(key) \
148  nndeploy::base::timeProfilerGetAverageTime(key)
149 #define NNDEPLOY_TIME_PROFILER_PRINT(title) \
150  nndeploy::base::timeProfilerPrint(title)
151 #define NNDEPLOY_TIME_PROFILER_PRINT_INDEX(title, index) \
152  nndeploy::base::timeProfilerPrintIndex(title, index)
153 #define NNDEPLOY_TIME_PROFILER_PRINT_REMOVE_WARMUP(title, warmup_times) \
154  nndeploy::base::timeProfilerPrintRemoveWarmup(title, warmup_times)
155 #define NNDEPLOY_AUTO_TIME nndeploy::base::AutoTime ___t(__LINE__, __func__)
156 #else
157 #define NNDEPLOY_TIME_PROFILER_RESET()
158 #define NNDEPLOY_TIME_POINT_START(key)
159 #define NNDEPLOY_TIME_POINT_END(key)
160 #define NNDEPLOY_TIME_PROFILER_GET_COST_TIME(key) -1.0f
161 #define NNDEPLOY_TIME_PROFILER_GET_AVERAGE_TIME(key) -1.0f
162 #define NNDEPLOY_TIME_PROFILER_PRINT(title)
163 #define NNDEPLOY_TIME_PROFILER_PRINT_INDEX(title, index)
164 #define NNDEPLOY_TIME_PROFILER_PRINT_REMOVE_WARMUP(title, warmup_times)
165 #define NNDEPLOY_AUTO_TIME
166 #endif
167 
168 #endif // _NNDEPLOY_BASE_TIME_PROFILER_H_
AutoTime & operator=(const AutoTime &)=delete
AutoTime & operator=(const AutoTime &&)=delete
AutoTime(const AutoTime &&)=delete
AutoTime(int line, const char *func)
AutoTime(const AutoTime &)=delete
void printIndex(const std::string &title, uint64_t index)
float getAverageTime(const std::string &key) const
void printRemoveWarmup(const std::string &title, uint64_t warmup_times)
float getCostTime(const std::string &key) const
void print(const std::string &title="")
void end(const std::string &key)
void start(const std::string &key)
Timer(const Timer &&)=delete
Timer(const Timer &)=delete
Timer & operator=(const Timer &&)=delete
Timer & operator=(const Timer &)=delete
uint64_t current() const
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
void timeProfilerPrint(const std::string &title="")
void timeProfilerReset()
float timeProfilerGetAverageTime(const std::string &key)
float timeProfilerGetCostTime(const std::string &key)
void timePointEnd(const std::string &key)
void timeProfilerPrintIndex(const std::string &title, uint64_t index)
void timeProfilerPrintRemoveWarmup(const std::string &title, uint64_t warmup_times)
void timePointStart(const std::string &key)