mlpack  2.0.1
timers.hpp
Go to the documentation of this file.
1 
15 #ifndef __MLPACK_CORE_UTILITIES_TIMERS_HPP
16 #define __MLPACK_CORE_UTILITIES_TIMERS_HPP
17 
18 #include <map>
19 #include <string>
20 
21 #if defined(__unix__) || defined(__unix)
22  #include <time.h> // clock_gettime()
23  #include <sys/time.h> // timeval, gettimeofday()
24  #include <unistd.h> // flags like _POSIX_VERSION
25 #elif defined(__MACH__) && defined(__APPLE__)
26  #include <mach/mach_time.h> // mach_timebase_info,
27  // mach_absolute_time()
28 
29  // TEMPORARY
30  #include <time.h> // clock_gettime()
31  #include <sys/time.h> // timeval, gettimeofday()
32  #include <unistd.h> // flags like _POSIX_VERSION
33 #elif defined(_WIN32)
34  #ifndef NOMINMAX
35  #define NOMINMAX // Don't define min and max macros.
36  #endif
37  #include <windows.h> // GetSystemTimeAsFileTime(),
38  // QueryPerformanceFrequency(),
39  // QueryPerformanceCounter()
40  #include <winsock.h> // timeval on windows
41  #undef NOMINMAX
42 
43  // uint64_t isn't defined on every windows.
44  #if !defined(HAVE_UINT64_T)
45  #if SIZEOF_UNSIGNED_LONG == 8
46  typedef unsigned long uint64_t;
47  #else
48  typedef unsigned long long uint64_t;
49  #endif // SIZEOF_UNSIGNED_LONG
50  #endif // HAVE_UINT64_T
51 
52  //gettimeofday has no equivalent will need to write extra code for that.
53  #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
54  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
55  #else
56  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
57  #endif // _MSC_VER, _MSC_EXTENSIONS
58 #else
59  #error "unknown OS"
60 #endif
61 
62 namespace mlpack {
63 
69 class Timer
70 {
71  public:
83  static void Start(const std::string& name);
84 
93  static void Stop(const std::string& name);
94 
100  static timeval Get(const std::string& name);
101 };
102 
103 class Timers
104 {
105  public:
107  Timers() { }
108 
112  std::map<std::string, timeval>& GetAllTimers();
113 
119  timeval GetTimer(const std::string& timerName);
120 
127  void PrintTimer(const std::string& timerName);
128 
137  void StartTimer(const std::string& timerName);
138 
145  void StopTimer(const std::string& timerName);
146 
152  bool GetState(std::string timerName);
153 
154  private:
156  std::map<std::string, timeval> timers;
158  std::map<std::string, bool> timerState;
159 
160  void FileTimeToTimeVal(timeval* tv);
161  void GetTime(timeval* tv);
162 };
163 
164 } // namespace mlpack
165 
166 #endif // __MLPACK_CORE_UTILITIES_TIMERS_HPP
std::map< std::string, bool > timerState
A map that contains whether or not each timer is currently running.
Definition: timers.hpp:158
void PrintTimer(const std::string &timerName)
Prints the specified timer.
Timers()
Nothing to do for the constructor.
Definition: timers.hpp:107
void FileTimeToTimeVal(timeval *tv)
timeval GetTimer(const std::string &timerName)
Returns a copy of the timer specified.
Linear algebra utility functions, generally performed on matrices or vectors.
std::map< std::string, timeval > timers
A map of all the timers that are being tracked.
Definition: timers.hpp:156
static void Start(const std::string &name)
Start the given timer.
static timeval Get(const std::string &name)
Get the value of the given timer.
void StartTimer(const std::string &timerName)
 * Initializes a timer, available like a normal value specified on  * the command line...
The timer class provides a way for mlpack methods to be timed.
Definition: timers.hpp:69
static void Stop(const std::string &name)
Stop the given timer.
void GetTime(timeval *tv)
bool GetState(std::string timerName)
Returns state of the given timer.
void StopTimer(const std::string &timerName)
 * Halts the timer, and replaces it's value with  * the delta time from it's start   *   *...
std::map< std::string, timeval > & GetAllTimers()
Returns a copy of all the timers used via this interface.