MLPACK  1.0.10
timers.hpp
Go to the documentation of this file.
1 
23 #ifndef __MLPACK_CORE_UTILITIES_TIMERS_HPP
24 #define __MLPACK_CORE_UTILITIES_TIMERS_HPP
25 
26 #include <map>
27 #include <string>
28 
29 #if defined(__unix__) || defined(__unix)
30  #include <time.h> // clock_gettime()
31  #include <sys/time.h> // timeval, gettimeofday()
32  #include <unistd.h> // flags like _POSIX_VERSION
33 #elif defined(__MACH__) && defined(__APPLE__)
34  #include <mach/mach_time.h> // mach_timebase_info,
35  // mach_absolute_time()
36 
37  // TEMPORARY
38  #include <time.h> // clock_gettime()
39  #include <sys/time.h> // timeval, gettimeofday()
40  #include <unistd.h> // flags like _POSIX_VERSION
41 #elif defined(_WIN32)
42  #include <windows.h> //GetSystemTimeAsFileTime(),
43  // QueryPerformanceFrequency(),
44  // QueryPerformanceCounter()
45  #include <winsock.h> //timeval on windows
46 
47  // uint64_t isn't defined on every windows.
48  #if !defined(HAVE_UINT64_T)
49  #if SIZEOF_UNSIGNED_LONG == 8
50  typedef unsigned long uint64_t;
51  #else
52  typedef unsigned long long uint64_t;
53  #endif // SIZEOF_UNSIGNED_LONG
54  #endif // HAVE_UINT64_T
55 
56  //gettimeofday has no equivalent will need to write extra code for that.
57  #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
58  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
59  #else
60  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
61  #endif // _MSC_VER, _MSC_EXTENSIONS
62 #else
63  #error "unknown OS"
64 #endif
65 
66 namespace mlpack {
67 
73 class Timer
74 {
75  public:
86  static void Start(const std::string& name);
87 
95  static void Stop(const std::string& name);
96 
102  static timeval Get(const std::string& name);
103 };
104 
105 class Timers
106 {
107  public:
109  Timers() { }
110 
114  std::map<std::string, timeval>& GetAllTimers();
115 
121  timeval GetTimer(const std::string& timerName);
122 
129  void PrintTimer(const std::string& timerName);
130 
139  void StartTimer(const std::string& timerName);
140 
147  void StopTimer(const std::string& timerName);
148 
149  private:
150  std::map<std::string, timeval> timers;
151 
152  void FileTimeToTimeVal(timeval* tv);
153  void GetTime(timeval* tv);
154 };
155 
156 }; // namespace mlpack
157 
158 #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:109
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:31
std::map< std::string, timeval > timers
Definition: timers.hpp:150
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:73
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.