NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Timer.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <chrono>
31 #include <iostream>
32 #include <string>
33 #include <thread>
34 #include <type_traits>
35 
36 #include "NumCpp/Core/Types.hpp"
37 
38 namespace nc
39 {
40  //================================================================================
42  template<typename TimeUnit = std::chrono::milliseconds>
43  class Timer
44  {
45  public:
46  //==============================Typedefs======================================
47  using ChronoClock = std::chrono::high_resolution_clock;
48  using TimePoint = std::chrono::time_point<ChronoClock>;
49 
50  //============================================================================
51  // Method Description:
54  Timer() :
55  start_(ChronoClock::now())
56  {
57  setUnits();
58  }
59 
60  //============================================================================
61  // Method Description:
66  explicit Timer(const std::string& inName) :
67  name_(inName + " "),
68  start_(ChronoClock::now())
69  {
70  setUnits();
71  }
72 
73  //============================================================================
74  // Method Description:
79  void setName(const std::string& inName)
80  {
81  name_ = inName + " ";
82  }
83 
84  //============================================================================
85  // Method Description:
90  void sleep(uint32 length)
91  {
92  std::this_thread::sleep_for(TimeUnit(length));
93  }
94 
95  //============================================================================
96  // Method Description:
99  void tic() noexcept
100  {
101  start_ = ChronoClock::now();
102  }
103 
104  //============================================================================
105  // Method Description:
112  uint64 toc(bool printElapsedTime = true)
113  {
114  const auto duration =
115  static_cast<uint64>(std::chrono::duration_cast<TimeUnit>(ChronoClock::now() - start_).count());
116 
117  if (printElapsedTime)
118  {
119  std::cout << name_ << "Elapsed Time = " << duration << unit_ << std::endl;
120  }
121 
122  return duration;
123  }
124 
125  private:
126  //==============================Attributes====================================
127  std::string name_{ "" };
128  std::string unit_{ "" };
129  TimePoint start_{};
130 
131  void setUnits()
132  {
133  if (std::is_same<TimeUnit, std::chrono::hours>::value)
134  {
135  unit_ = " hours";
136  }
137  else if (std::is_same<TimeUnit, std::chrono::minutes>::value)
138  {
139  unit_ = " minutes";
140  }
141  else if (std::is_same<TimeUnit, std::chrono::seconds>::value)
142  {
143  unit_ = " seconds";
144  }
145  else if (std::is_same<TimeUnit, std::chrono::milliseconds>::value)
146  {
147  unit_ = " milliseconds";
148  }
149  else if (std::is_same<TimeUnit, std::chrono::microseconds>::value)
150  {
151  unit_ = " microseconds";
152  }
153  else if (std::is_same<TimeUnit, std::chrono::nanoseconds>::value)
154  {
155  unit_ = " nanoseconds";
156  }
157  else
158  {
159  unit_ = " time units of some sort";
160  }
161  }
162  };
163 } // namespace nc
A timer class for timing code execution.
Definition: Timer.hpp:44
std::chrono::time_point< ChronoClock > TimePoint
Definition: Timer.hpp:48
uint64 toc(bool printElapsedTime=true)
Definition: Timer.hpp:112
void tic() noexcept
Definition: Timer.hpp:99
Timer(const std::string &inName)
Definition: Timer.hpp:66
Timer()
Definition: Timer.hpp:54
void setName(const std::string &inName)
Definition: Timer.hpp:79
std::chrono::high_resolution_clock ChronoClock
Definition: Timer.hpp:47
void sleep(uint32 length)
Definition: Timer.hpp:90
Definition: Coordinate.hpp:45
std::uint64_t uint64
Definition: Types.hpp:39
std::uint32_t uint32
Definition: Types.hpp:40