mlpack  1.0.12
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  #include <windows.h> //GetSystemTimeAsFileTime(),
35  // QueryPerformanceFrequency(),
36  // QueryPerformanceCounter()
37  #include <winsock.h> //timeval on windows
38 
39  // uint64_t isn't defined on every windows.
40  #if !defined(HAVE_UINT64_T)
41  #if SIZEOF_UNSIGNED_LONG == 8
42  typedef unsigned long uint64_t;
43  #else
44  typedef unsigned long long uint64_t;
45  #endif // SIZEOF_UNSIGNED_LONG
46  #endif // HAVE_UINT64_T
47 
48  //gettimeofday has no equivalent will need to write extra code for that.
49  #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
50  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
51  #else
52  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
53  #endif // _MSC_VER, _MSC_EXTENSIONS
54 #else
55  #error "unknown OS"
56 #endif
57 
58 namespace mlpack {
59 
65 class Timer
66 {
67  public:
78  static void Start(const std::string& name);
79 
87  static void Stop(const std::string& name);
88 
94  static timeval Get(const std::string& name);
95 };
96 
97 class Timers
98 {
99  public:
101  Timers() { }
102 
106  std::map<std::string, timeval>& GetAllTimers();
107 
113  timeval GetTimer(const std::string& timerName);
114 
121  void PrintTimer(const std::string& timerName);
122 
131  void StartTimer(const std::string& timerName);
132 
139  void StopTimer(const std::string& timerName);
140 
141  private:
142  std::map<std::string, timeval> timers;
143 
144  void FileTimeToTimeVal(timeval* tv);
145  void GetTime(timeval* tv);
146 };
147 
148 }; // namespace mlpack
149 
150 #endif // __MLPACK_CORE_UTILITIES_TIMERS_HPP
void PrintTimer(const std::string &timerName)
Prints the specified timer.
Timers()
Nothing to do for the constructor.
Definition: timers.hpp:101
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.
Definition: load.hpp:23
std::map< std::string, timeval > timers
Definition: timers.hpp:142
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:65
static void Stop(const std::string &name)
Stop the given timer.
void GetTime(timeval *tv)
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.