NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
Timer.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <chrono>
31#include <iostream>
32#include <string>
33#include <thread>
34#include <type_traits>
35
36#include "NumCpp/Core/Enums.hpp"
37#include "NumCpp/Core/Types.hpp"
38
39namespace nc
40{
41 //================================================================================
43 template<typename TimeUnit = std::chrono::milliseconds>
44 class Timer
45 {
46 public:
47 //==============================Typedefs======================================
48 using ChronoClock = std::chrono::steady_clock;
49 using TimePoint = std::chrono::time_point<ChronoClock>;
50
51 //============================================================================
52 // Method Description:
56 start_(ChronoClock::now())
57 {
58 setUnits();
59 }
60
61 //============================================================================
62 // Method Description:
67 explicit Timer(const std::string& inName) :
68 name_(inName + " "),
69 start_(ChronoClock::now())
70 {
71 setUnits();
72 }
73
74 //============================================================================
75 // Method Description:
80 void setName(const std::string& inName)
81 {
82 name_ = inName + " ";
83 }
84
85 //============================================================================
86 // Method Description:
91 void sleep(uint32 length)
92 {
93 std::this_thread::sleep_for(TimeUnit(length));
94 }
95
96 //============================================================================
97 // Method Description:
100 void tic() noexcept
101 {
102 start_ = ChronoClock::now();
103 }
104
105 //============================================================================
106 // Method Description:
113 TimeUnit toc(PrintElapsedTime printElapsedTime = PrintElapsedTime::YES)
114 {
115 const auto duration = std::chrono::duration_cast<TimeUnit>(ChronoClock::now() - start_);
116
117 if (printElapsedTime == PrintElapsedTime::YES)
118 {
119 std::cout << name_ << "Elapsed Time = " << duration.count() << 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 constexpr (std::is_same_v<TimeUnit, std::chrono::hours>)
134 {
135 unit_ = " hours";
136 }
137 else if constexpr (std::is_same_v<TimeUnit, std::chrono::minutes>)
138 {
139 unit_ = " minutes";
140 }
141 else if constexpr (std::is_same_v<TimeUnit, std::chrono::seconds>)
142 {
143 unit_ = " seconds";
144 }
145 else if constexpr (std::is_same_v<TimeUnit, std::chrono::milliseconds>)
146 {
147 unit_ = " milliseconds";
148 }
149 else if constexpr (std::is_same_v<TimeUnit, std::chrono::microseconds>)
150 {
151 unit_ = " microseconds";
152 }
153 else if constexpr (std::is_same_v<TimeUnit, std::chrono::nanoseconds>)
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:45
std::chrono::time_point< ChronoClock > TimePoint
Definition: Timer.hpp:49
std::chrono::steady_clock ChronoClock
Definition: Timer.hpp:48
void tic() noexcept
Definition: Timer.hpp:100
Timer(const std::string &inName)
Definition: Timer.hpp:67
Timer()
Definition: Timer.hpp:55
void setName(const std::string &inName)
Definition: Timer.hpp:80
void sleep(uint32 length)
Definition: Timer.hpp:91
TimeUnit toc(PrintElapsedTime printElapsedTime=PrintElapsedTime::YES)
Definition: Timer.hpp:113
Definition: Cartesian.hpp:40
PrintElapsedTime
Print Elapsed Time boolean.
Definition: Enums.hpp:110
std::uint32_t uint32
Definition: Types.hpp:40