NiHu  2.0
timer.h
Go to the documentation of this file.
1 
4 #ifndef TIMER_H_INCLUDED
5 #define TIMER_H_INCLUDED
6 
7 #include <boost/predef.h>
8 
9 #if BOOST_OS_WINDOWS
10 #ifndef NOMINMAX
11 #define NOMINMAX // otherwise std::max and std::min is defined
12 #endif
13 #include <windows.h>
14 #else
15 #include <ctime>
16 #endif
17 
18 #include <chrono>
19 #include <stdexcept>
20 
21 namespace NiHu
22 {
23 
25 class wc_time
26 {
27  typedef std::chrono::system_clock clock_t;
28 
29 public:
31  typedef std::chrono::time_point<clock_t> time_point_t;
32 
34  static time_point_t tic()
35  {
36  return clock_t::now();
37  }
38 
42  static double toc(time_point_t const &t0)
43  {
44  std::chrono::duration<double, std::nano> dur = tic() - t0;
45  return dur.count() / 1e9;
46  }
47 };
48 
49 
51 class cpu_time
52 {
53 public:
54 #if BOOST_OS_WINDOWS
55  typedef double time_point_t;
57 
59  static time_point_t tic()
60  {
61  FILETIME createTime;
62  FILETIME exitTime;
63  FILETIME kernelTime;
64  FILETIME userTime;
65  if (GetProcessTimes(GetCurrentProcess(),
66  &createTime, &exitTime, &kernelTime, &userTime) == -1)
67  throw std::runtime_error("Could not get CPU time point");
68 
69  SYSTEMTIME userSystemTime;
70  if (FileTimeToSystemTime(&userTime, &userSystemTime) == -1)
71  throw std::runtime_error("Could not get CPU time point");
72 
73  return (double)userSystemTime.wHour * 3600.0 +
74  (double)userSystemTime.wMinute * 60.0 +
75  (double)userSystemTime.wSecond +
76  (double)userSystemTime.wMilliseconds / 1000.0;
77  }
78 
82  static double toc(time_point_t const &t0)
83  {
84  return tic() - t0;
85  }
86 
87 #else
88 
89  typedef clock_t time_point_t;
90 
91  static time_point_t tic()
92  {
93  return std::clock();
94  }
95 
96  static double toc(time_point_t const &t0)
97  {
98  return double(tic() - t0) / CLOCKS_PER_SEC;
99  }
100 
101 #endif
102 };
103 
104 } // end of namespace NiHu
105 
106 #endif
NiHu::wc_time::toc
static double toc(time_point_t const &t0)
returns the time elapsed since t0
Definition: timer.h:42
NiHu::cpu_time
CPU time.
Definition: timer.h:51
NiHu::wc_time::time_point_t
std::chrono::time_point< clock_t > time_point_t
the time point type returned by tic
Definition: timer.h:31
NiHu::wc_time
wall clock time
Definition: timer.h:25
NiHu::wc_time::tic
static time_point_t tic()
returns the current time point
Definition: timer.h:34