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 <map>
18 #include <string>
19 #include <chrono> // chrono library for cross platform timer calculation.
20 #include <thread> // std::thread is used for thread safety.
21 #include <mutex>
22 #include <list>
23 #include <atomic>
24 
25 #if defined(_WIN32)
26  // uint64_t isn't defined on every windows.
27  #if !defined(HAVE_UINT64_T)
28  #if SIZEOF_UNSIGNED_LONG == 8
29  typedef unsigned long uint64_t;
30  #else
31  typedef unsigned long long uint64_t;
32  #endif // SIZEOF_UNSIGNED_LONG
33  #endif // HAVE_UINT64_T
34 #endif
35 
36 namespace mlpack {
37 
45 class Timer
46 {
47  public:
59  static void Start(const std::string& name);
60 
69  static void Stop(const std::string& name);
70 
76  static std::chrono::microseconds Get(const std::string& name);
77 
82  static void EnableTiming();
83 
88  static void DisableTiming();
89 
94  static void ResetAll();
95 };
96 
97 class Timers
98 {
99  public:
101  Timers() : enabled(false) { }
102 
106  std::map<std::string, std::chrono::microseconds> GetAllTimers();
107 
112  void Reset();
113 
120  std::chrono::microseconds GetTimer(const std::string& timerName);
121 
128  void PrintTimer(const std::string& timerName);
129 
139  void StartTimer(const std::string& timerName,
140  const std::thread::id& threadId = std::thread::id());
141 
148  void StopTimer(const std::string& timerName,
149  const std::thread::id& threadId = std::thread::id());
150 
157  bool GetState(const std::string& timerName,
158  const std::thread::id& threadId = std::thread::id());
159 
163  void StopAllTimers();
164 
166  std::atomic<bool>& Enabled() { return enabled; }
168  bool Enabled() const { return enabled; }
169 
170  private:
172  std::map<std::string, std::chrono::microseconds> timers;
174  std::mutex timersMutex;
176  std::map<std::thread::id, std::map<std::string,
177  std::chrono::high_resolution_clock::time_point>> timerStartTime;
178 
180  std::atomic<bool> enabled;
181 };
182 
183 } // namespace mlpack
184 
185 #endif // MLPACK_CORE_UTILITIES_TIMERS_HPP
std::atomic< bool > & Enabled()
Modify whether or not timing is enabled.
Definition: timers.hpp:166
static std::chrono::microseconds Get(const std::string &name)
Get the value of the given timer.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_po.hpp:21
static void Stop(const std::string &name)
Stop the given timer.
static void EnableTiming()
Enable timing of mlpack programs.
The timer class provides a way for mlpack methods to be timed.
Definition: timers.hpp:45
static void ResetAll()
Stop and reset all running timers.
bool Enabled() const
Get whether or not timing is enabled.
Definition: timers.hpp:168
Timers()
Default to disabled.
Definition: timers.hpp:101
static void DisableTiming()
Disable timing of mlpack programs.
static void Start(const std::string &name)
Start the given timer.