NumCpp  2.1.0
A C++ implementation of the Python Numpy library
Timer.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/Core/Types.hpp"
32 
33 #include <chrono>
34 #include <iostream>
35 #include <string>
36 #include <thread>
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  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 = static_cast<uint64>(std::chrono::duration_cast<TimeUnit>(ChronoClock::now() - start_).count());
115 
116  if (printElapsedTime)
117  {
118  std::cout << name_ << "Elapsed Time = " << duration << unit_ << std::endl;
119  }
120 
121  return duration;
122  }
123 
124  private:
125  //==============================Attributes====================================
126  std::string name_{ "" };
127  std::string unit_{ "" };
128  TimePoint start_{};
129 
130  void setUnits()
131  {
132  if (std::is_same<TimeUnit, std::chrono::hours>::value)
133  {
134  unit_ = " hours";
135  }
136  else if (std::is_same<TimeUnit, std::chrono::minutes>::value)
137  {
138  unit_ = " minutes";
139  }
140  else if (std::is_same<TimeUnit, std::chrono::seconds>::value)
141  {
142  unit_ = " seconds";
143  }
144  else if (std::is_same<TimeUnit, std::chrono::milliseconds>::value)
145  {
146  unit_ = " milliseconds";
147  }
148  else if (std::is_same<TimeUnit, std::chrono::microseconds>::value)
149  {
150  unit_ = " microseconds";
151  }
152  else if (std::is_same<TimeUnit, std::chrono::nanoseconds>::value)
153  {
154  unit_ = " nanoseconds";
155  }
156  else
157  {
158  unit_ = " time units of some sort";
159  }
160  }
161  };
162 } // namespace nc
nc::Timer::toc
uint64 toc(bool printElapsedTime=true)
Definition: Timer.hpp:112
nc::uint64
std::uint64_t uint64
Definition: Types.hpp:40
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc::Timer::TimePoint
std::chrono::time_point< ChronoClock > TimePoint
Definition: Timer.hpp:48
nc::Timer::sleep
void sleep(uint32 length)
Definition: Timer.hpp:90
nc
Definition: Coordinate.hpp:45
nc::Timer::Timer
Timer()
Definition: Timer.hpp:54
nc::Timer::Timer
Timer(const std::string &inName)
Definition: Timer.hpp:66
Types.hpp
nc::Timer::ChronoClock
std::chrono::high_resolution_clock ChronoClock
Definition: Timer.hpp:47
nc::Timer::setName
void setName(const std::string &inName)
Definition: Timer.hpp:79
nc::Timer
A timer class for timing code execution.
Definition: Timer.hpp:43
nc::Timer::tic
void tic() noexcept
Definition: Timer.hpp:99