Manak  2.0.0
timer.hpp
Go to the documentation of this file.
1 
8 #ifndef MANAK_UTIL_TIMER_HPP_INCLUDED
9 #define MANAK_UTIL_TIMER_HPP_INCLUDED
10 
11 #include <limits>
12 #include <iostream>
13 #include <vector>
14 
15 #include "pmeasure.hpp"
16 #include "macro_utils.hpp"
17 
18 #if defined(__unix__) || defined(__unix)
19  #include <time.h> // clock_gettime()
20  #include <sys/time.h> // timeval, gettimeofday()
21  #include <unistd.h> // flags like _POSIX_VERSION
22 
23 #elif defined(__MACH__) && defined(__APPLE__)
24  #include <mach/mach_time.h> // mach_timebase_info,
25  // mach_absolute_time()
26  // TEMPORARY
27  #include <time.h> // clock_gettime()
28  #include <sys/time.h> // timeval, gettimeofday()
29  #include <unistd.h> // flags like _POSIX_VERSION
30 
31 #elif defined(_WIN32)
32  #include <windows.h> //GetSystemTimeAsFileTime(),
33  // QueryPerformanceFrequency(),
34  // QueryPerformanceCounter()
35  #include <winsock.h> //timeval on windows
36 
37  // uint64_t isn't defined on every windows.
38  #if !defined(HAVE_UINT64_T)
39  #if SIZEOF_UNSIGNED_LONG == 8
40  typedef unsigned long uint64_t;
41  #else
42  typedef unsigned long long uint64_t;
43  #endif // SIZEOF_UNSIGNED_LONG
44  #endif // HAVE_UINT64_T
45 
46  //gettimeofday has no equivalent will need to write extra code for that.
47  #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
48  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
49  #else
50  #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
51  #endif // _MSC_VER, _MSC_EXTENSIONS
52 
53  #ifdef min
54  #undef min
55  #endif // min
56  #ifdef max
57  #undef max
58  #endif // max
59 #else
60  #error "unknown OS"
61 #endif
62 
63 
64 // On Windows machines, we need to define timersub.
65 #ifdef _WIN32
66 inline void timersub(const timeval* tvp, const timeval* uvp, timeval* vvp)
67 {
68  vvp->tv_sec = tvp->tv_sec - uvp->tv_sec;
69  vvp->tv_usec = tvp->tv_usec - uvp->tv_usec;
70  if (vvp->tv_usec < 0)
71  {
72  --vvp->tv_sec;
73  vvp->tv_usec += 1000000;
74  }
75 }
76 #endif
77 
78 namespace manak
79 {
80 
84 class Timer
85 {
86  public:
92  static uint64_t microtimer()
93  {
94  timeval tv;
95 
96  #if defined(__unix__) || defined(__unix)
97  gettimeofday (&tv, NULL);
98 
99  #elif defined(_WIN32)
100  static double frequency = 0.0;
101  static LARGE_INTEGER offset;
102 
103  // If this is the first time we've run, get the frequency.
104  // We use frequency == 0.0 to indicate that
105  // QueryPerformanceFrequency is uninitialised.
106  if (frequency == 0.0)
107  {
108  LARGE_INTEGER pF;
109  if (!QueryPerformanceFrequency(&pF))
110  {
111  // Fallback for the QueryPerformanceCounter function.
112  //FileTimeToTimeVal(tv);
113  }
114  else
115  {
116  QueryPerformanceCounter(&offset);
117  frequency = (double)pF.QuadPart / 1000000.0;
118  }
119  }
120 
121  if (frequency != 0.0)
122  {
123  LARGE_INTEGER pC;
124  // Get the current performance-counter value.
125  QueryPerformanceCounter(&pC);
126 
127  pC.QuadPart -= offset.QuadPart;
128  double microseconds = (double)pC.QuadPart / frequency;
129  pC.QuadPart = microseconds;
130  tv.tv_sec = (long)pC.QuadPart / 1000000;
131  tv.tv_usec = (long)(pC.QuadPart % 1000000);
132  }
133 
134  #endif
135 
136  return tv.tv_sec * 1000000 + tv.tv_usec;
137  }
138 
140  MANAK_INLINE static void Initialize();
141 
143  MANAK_INLINE static PMeasure Deinitialize(size_t iterations);
144 
146  MANAK_INLINE static void StartIter();
147 
149  MANAK_INLINE static void StartTimer();
150 
152  MANAK_INLINE static void StopTimer();
153 
155  MANAK_INLINE static bool EndIter(size_t& iterations);
156 
158  MANAK_INLINE static PMeasure GetStats(size_t iterations);
159 
161  MANAK_INLINE static uint64_t& TotalTime();
162 
164  MANAK_INLINE static uint64_t& CTime();
165 
167  MANAK_INLINE static uint64_t& Start();
168 
170  MANAK_INLINE static uint64_t& Min();
171 
173  MANAK_INLINE static uint64_t& Max();
174 
176  MANAK_INLINE static bool& StateIter();
177 
179  MANAK_INLINE static bool& StateTimer();
180 
182  MANAK_INLINE static bool& StateInit();
183 
185  MANAK_INLINE static void replaceAll(std::string& str,
186  const std::string& from,
187  const std::string& to);
188 
194  MANAK_INLINE static std::string getTimeStamp(bool removeSpaces = false);
195 }; // class Timer
196 
197 }; // namespace manak
198 
199 #ifndef MANAK_USE_DYN_LINK
200 #include "timer.cpp"
201 #endif // MANAK_USE_DYN_LINK
202 
203 #endif // MANAK_UTIL_TIMER_HPP_INCLUDED
static uint64_t & CTime()
Get-set current timing.
static void StartTimer()
Start timer.
static uint64_t microtimer()
Returns the time in microseconds.
Definition: timer.hpp:92
static bool EndIter(size_t &iterations)
Mark end of the iteration.
static PMeasure Deinitialize(size_t iterations)
Deinitialize and register.
#define MANAK_INLINE
Check if user has opt for dynamic linking.
Definition: macro_utils.hpp:25
static void Initialize()
Initialize a new Timer instance.
Stores the timing information of any run.
Definition: pmeasure.hpp:19
static uint64_t & Min()
Get-set min time.
This class handles all the time related queries of libGDL.
Definition: timer.hpp:84
static void replaceAll(std::string &str, const std::string &from, const std::string &to)
Replace all occurrences.
static uint64_t & Max()
Get-set max time.
static bool & StateInit()
Get-set initialization state.
static bool & StateIter()
Get-set iteration state.
static PMeasure GetStats(size_t iterations)
Get statistics of current timer instance.
static bool & StateTimer()
Get-set timer state.
static uint64_t & TotalTime()
Get-set total time.
static uint64_t & Start()
Get-set start time.
static void StopTimer()
Stop timer.
static std::string getTimeStamp(bool removeSpaces=false)
Returns current time-stamp as string.
static void StartIter()
Mark start of the iteration.