OpenFPM_data  0.1.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Namespaces Functions Variables Typedefs Friends
timer.hpp
1 //
2 // Timer.h
3 //
4 //
5 
6 #ifndef TIMER_HPP
7 #define TIMER_HPP
8 
9 #include <time.h>
10 #include <sys/time.h>
11 
12 #ifdef __MACH__
13 #include <mach/clock.h>
14 #include <mach/mach.h>
15 #endif
16 
25 class timer
26 {
27  // Flag that indicate if the timer is running or not
28  bool running;
29 
30  struct timespec tsstart;
31  clock_t cstart;
32 
33  struct timespec tsstop;
34  clock_t cstop;
35 
36  // Fill the stop point
37  void check()
38  {
39 
40 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
41  clock_serv_t cclock;
42  mach_timespec_t mts;
43  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
44  clock_get_time(cclock, &mts);
45  mach_port_deallocate(mach_task_self(), cclock);
46  tsstop.tv_sec = mts.tv_sec;
47  tsstop.tv_nsec = mts.tv_nsec;
48 #else
49  clock_gettime(CLOCK_REALTIME, &tsstop);
50 #endif
51  cstop = clock();
52  }
53 
54 public:
55 
58  :running(false),cstart(0),cstop(0)
59  {
60  tsstart = timespec();
61  tsstop = timespec();
62  }
63 
67  void start()
68  {
69  // time is running
70  running = true;
71 
72 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
73  clock_serv_t cclock;
74  mach_timespec_t mts;
75  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
76  clock_get_time(cclock, &mts);
77  mach_port_deallocate(mach_task_self(), cclock);
78  tsstart.tv_sec = mts.tv_sec;
79  tsstart.tv_nsec = mts.tv_nsec;
80 #else
81  clock_gettime(CLOCK_REALTIME, &tsstart);
82 #endif
83  cstart = clock();
84 
85  }
86 
91  void stop()
92  {
93  if (running == false) return;
94  running = false;
95  check();
96  }
97 
102  double getwct()
103  {
104  if (running == true)
105  check();
106 
107  return ((double)(tsstop.tv_sec - tsstart.tv_sec)) + ((1e-9) * ((double)(tsstop.tv_nsec - tsstart.tv_nsec)));
108  }
109 
114  double getcputime()
115  {
116  if (running == true)
117  check();
118 
119  return (((double)(cstop - cstart)) / CLOCKS_PER_SEC);
120  }
121 
126  void reset()
127  {
128  tsstart = tsstop;
129  cstart = cstop;
130  }
131 };
132 
133 #endif
134 
135 
timer()
Default constructor.
Definition: timer.hpp:57
double getwct()
Return the elapsed real time.
Definition: timer.hpp:102
double getcputime()
Return the cpu time.
Definition: timer.hpp:114
void start()
Start the timer.
Definition: timer.hpp:67
void reset()
Reset the timer.
Definition: timer.hpp:126
Class for cpu time benchmarking.
Definition: timer.hpp:25
void stop()
Stop the timer.
Definition: timer.hpp:91