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