timers.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_CORE_UTILITIES_TIMERS_HPP
15 #define MLPACK_CORE_UTILITIES_TIMERS_HPP
16 
17 #include <atomic>
18 #include <chrono> // chrono library for cross platform timer calculation.
19 #include <iomanip>
20 #include <list>
21 #include <map>
22 #include <mutex>
23 #include <string>
24 #include <thread> // std::thread is used for thread safety.
25 
26 #if defined(_WIN32)
27  // uint64_t isn't defined on every windows.
28  #if !defined(HAVE_UINT64_T)
29  #if SIZEOF_UNSIGNED_LONG == 8
30  typedef unsigned long uint64_t;
31  #else
32  typedef unsigned long long uint64_t;
33  #endif // SIZEOF_UNSIGNED_LONG
34  #endif // HAVE_UINT64_T
35 #endif
36 
37 namespace mlpack {
38 
46 class Timer
47 {
48  public:
60  static void Start(const std::string& name);
61 
70  static void Stop(const std::string& name);
71 
77  static std::chrono::microseconds Get(const std::string& name);
78 
83  static void EnableTiming();
84 
89  static void DisableTiming();
90 
95  static void ResetAll();
96 
100  static std::map<std::string, std::chrono::microseconds> GetAllTimers();
101 };
102 
103 namespace util {
104 
105 class Timers
106 {
107  public:
109  Timers() : enabled(false) { }
110 
114  std::map<std::string, std::chrono::microseconds> GetAllTimers();
115 
120  void Reset();
121 
128  std::chrono::microseconds Get(const std::string& timerName);
129 
136  static std::string Print(const std::chrono::microseconds& totalDuration);
137 
147  void Start(const std::string& timerName,
148  const std::thread::id& threadId = std::thread::id());
149 
156  void Stop(const std::string& timerName,
157  const std::thread::id& threadId = std::thread::id());
158 
162  void StopAllTimers();
163 
165  std::atomic<bool>& Enabled() { return enabled; }
167  bool Enabled() const { return enabled; }
168 
169  private:
171  std::map<std::string, std::chrono::microseconds> timers;
173  std::mutex timersMutex;
175  std::map<std::thread::id, std::map<std::string,
176  std::chrono::high_resolution_clock::time_point>> timerStartTime;
177 
179  std::atomic<bool> enabled;
180 };
181 
182 } // namespace util
183 } // namespace mlpack
184 
185 #endif // MLPACK_CORE_UTILITIES_TIMERS_HPP
static std::chrono::microseconds Get(const std::string &name)
Get the value of the given timer.
bool Enabled() const
Get whether or not timing is enabled.
Definition: timers.hpp:167
Linear algebra utility functions, generally performed on matrices or vectors.
static void Stop(const std::string &name)
Stop the given timer.
static std::map< std::string, std::chrono::microseconds > GetAllTimers()
Returns a copy of all the timers used via this interface.
Timers()
Default to disabled.
Definition: timers.hpp:109
static void EnableTiming()
Enable timing of mlpack programs.
The timer class provides a way for mlpack methods to be timed.
Definition: timers.hpp:46
static void ResetAll()
Stop and reset all running timers.
static void DisableTiming()
Disable timing of mlpack programs.
static void Start(const std::string &name)
Start the given timer.
std::atomic< bool > & Enabled()
Modify whether or not timing is enabled.
Definition: timers.hpp:165