xref: /aosp_15_r20/external/pytorch/caffe2/core/timer.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #ifndef CAFFE2_CORE_TIMER_H_
2 #define CAFFE2_CORE_TIMER_H_
3 
4 #include <chrono>
5 
6 #include "caffe2/core/common.h"
7 
8 namespace caffe2 {
9 
10 /**
11  * @brief A simple timer object for measuring time.
12  *
13  * This is a minimal class around a std::chrono::high_resolution_clock that
14  * serves as a utility class for testing code.
15  */
16 class Timer {
17  public:
18   typedef std::chrono::high_resolution_clock clock;
19   typedef std::chrono::nanoseconds ns;
Timer()20   Timer() { Start(); }
21   /**
22    * @brief Starts a timer.
23    */
Start()24   inline void Start() { start_time_ = clock::now(); }
NanoSeconds()25   inline float NanoSeconds() {
26     return static_cast<float>(
27         std::chrono::duration_cast<ns>(clock::now() - start_time_).count());
28   }
29   /**
30    * @brief Returns the elapsed time in milliseconds.
31    */
MilliSeconds()32   inline float MilliSeconds() { return NanoSeconds() / 1000000.f; }
33   /**
34    * @brief Returns the elapsed time in microseconds.
35    */
MicroSeconds()36   inline float MicroSeconds() { return NanoSeconds() / 1000.f; }
37   /**
38    * @brief Returns the elapsed time in seconds.
39    */
Seconds()40   inline float Seconds() { return NanoSeconds() / 1000000000.f; }
41 
42  protected:
43   std::chrono::time_point<clock> start_time_;
44   C10_DISABLE_COPY_AND_ASSIGN(Timer);
45 };
46 }
47 
48 #endif  // CAFFE2_CORE_TIMER_H_
49