OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
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 {
28  bool running;
29 
31  struct timespec tsstart;
32 
34  clock_t cstart;
35 
36  // stop time
37  struct timespec tsstop;
38 
40  clock_t cstop;
41 
42  // Fill the stop point
43  void check()
44  {
45 
46 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
47  clock_serv_t cclock;
48  mach_timespec_t mts;
49  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
50  clock_get_time(cclock, &mts);
51  mach_port_deallocate(mach_task_self(), cclock);
52  tsstop.tv_sec = mts.tv_sec;
53  tsstop.tv_nsec = mts.tv_nsec;
54 #else
55  clock_gettime(CLOCK_REALTIME, &tsstop);
56 #endif
57  cstop = clock();
58  }
59 
60 public:
61 
64  :running(false),cstart(0),cstop(0)
65  {
66  tsstart = timespec();
67  tsstop = timespec();
68  }
69 
73  void start()
74  {
75  // time is running
76  running = true;
77 
78 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
79  clock_serv_t cclock;
80  mach_timespec_t mts;
81  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
82  clock_get_time(cclock, &mts);
83  mach_port_deallocate(mach_task_self(), cclock);
84  tsstart.tv_sec = mts.tv_sec;
85  tsstart.tv_nsec = mts.tv_nsec;
86 #else
87  clock_gettime(CLOCK_REALTIME, &tsstart);
88 #endif
89  cstart = clock();
90 
91  }
92 
97  void stop()
98  {
99  if (running == false) return;
100  running = false;
101  check();
102  }
103 
108  double getwct()
109  {
110  if (running == true)
111  check();
112 
113  return ((double)(tsstop.tv_sec - tsstart.tv_sec)) + ((1e-9) * ((double)(tsstop.tv_nsec - tsstart.tv_nsec)));
114  }
115 
120  double getcputime()
121  {
122  if (running == true)
123  check();
124 
125  return (((double)(cstop - cstart)) / CLOCKS_PER_SEC);
126  }
127 
132  void reset()
133  {
134  tsstart = tsstop;
135  cstart = cstop;
136  }
137 };
138 
139 #endif
140 
141 
struct timespec tsstart
starting time
Definition: timer.hpp:31
timer()
Default constructor.
Definition: timer.hpp:63
double getwct()
Return the elapsed real time.
Definition: timer.hpp:108
double getcputime()
Return the cpu time.
Definition: timer.hpp:120
bool running
Flag that indicate if the timer is running or not.
Definition: timer.hpp:28
clock_t cstart
start time from epoch
Definition: timer.hpp:34
clock_t cstop
stop time from epoch
Definition: timer.hpp:40
void start()
Start the timer.
Definition: timer.hpp:73
void reset()
Reset the timer.
Definition: timer.hpp:132
Class for cpu time benchmarking.
Definition: timer.hpp:25
void stop()
Stop the timer.
Definition: timer.hpp:97