DISPLACE  1.0
A spatial model of fisheries to help sustainable fishing and maritime spatial planning
profiler.h
Go to the documentation of this file.
1 /* --------------------------------------------------------------------------
2  * DISPLACE: DYNAMIC INDIVIDUAL VESSEL-BASED SPATIAL PLANNING
3  * AND EFFORT DISPLACEMENT
4  * Copyright (c) 2012, 2013, 2014 Francois Bastardie <fba@aqua.dtu.dk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  * --------------------------------------------------------------------------
20  */
21 #ifndef PROFILER_H
22 #define PROFILER_H
23 
24 // profiling is unsupported on MsVC.
25 #if defined (PROFILE) && defined (_MSC_VER)
26 #undef PROFILE
27 #endif
28 
29 #include <time.h>
30 
31 class Profiler {
32 private:
33 #ifdef PROFILE
34  struct timespec mstart;
35 #endif
36 
37 public:
39 #ifdef PROFILE
40  clock_gettime(CLOCK_REALTIME, &mstart);
41 #endif
42  }
43 
44  double elapsed_ms(bool cumulative = false) {
45 #ifdef PROFILE
46  struct timespec now;
47  clock_gettime(CLOCK_REALTIME, &now);
48  double frm = (double)mstart.tv_sec + (double)mstart.tv_nsec * 1e-9;
49  double t = (double)now.tv_sec + (double)now.tv_nsec * 1e-9;
50 
51  if (!cumulative)
52  mstart = now;
53  return t-frm;
54 #else
55  (void)cumulative;
56  return 0.0;
57 #endif
58  }
59 
60  void start() {
61 #ifdef PROFILE
62  clock_gettime(CLOCK_REALTIME, &mstart);
63 #endif
64  }
65 
66 };
67 
69 private:
70 #ifdef PROFILE
71  struct timespec mstart;
72  double mcumtime, mMin, mMax;
73  int mcumrun;
74 #else
75  static constexpr double mMin = 0.0, mMax = 0.0;
76 #endif
77 
78 public:
80 #ifdef PROFILE
81  : mstart(), mcumtime(0.0), mMin(0), mMax(0), mcumrun(0)
82 #endif
83  {
84 #ifdef PROFILE
85  clock_gettime(CLOCK_REALTIME, &mstart);
86 #endif
87  }
88 
89  double elapsed_ms() {
90 #ifdef PROFILE
91  struct timespec now;
92  clock_gettime(CLOCK_REALTIME, &now);
93  double frm = (double)mstart.tv_sec + (double)mstart.tv_nsec * 1e-9;
94  double t = (double)now.tv_sec + (double)now.tv_nsec * 1e-9 - frm;
95  mstart = now;
96  mcumtime += t;
97  if (mcumrun == 0) {
98  mMin = t;
99  mMax = t;
100  } else {
101  mMin = std::min(mMin,t);
102  mMax = std::max(mMax,t);
103  }
104 
105  ++mcumrun;
106  return t;
107 #else
108  return 0.0;
109 #endif
110  }
111 
112  void start() {
113 #ifdef PROFILE
114  clock_gettime(CLOCK_REALTIME, &mstart);
115 #endif
116  }
117 
118  int runs() {
119 #ifdef PROFILE
120  return mcumrun;
121 #else
122  return 0;
123 #endif
124  }
125 
126  double avg() {
127 #ifdef PROFILE
128  return mcumtime / (double)mcumrun;
129 #else
130  return 0.0;
131 #endif
132  }
133 
134  // double min() const { return mMin; }
135  // double max() const { return mMax; }
136 
137  double total() {
138 #ifdef PROFILE
139  return mcumtime;
140 #else
141  return 0.0;
142 #endif
143  }
144 };
145 
146 #endif // PROFILER_H
double avg()
Definition: profiler.h:126
void start()
Definition: profiler.h:60
Definition: profiler.h:31
void start()
Definition: profiler.h:112
double elapsed_ms(bool cumulative=false)
Definition: profiler.h:44
double total()
Definition: profiler.h:137
int runs()
Definition: profiler.h:118
Profiler()
Definition: profiler.h:38
AverageProfiler()
Definition: profiler.h:79
Definition: profiler.h:68
double elapsed_ms()
Definition: profiler.h:89