3232#include < string>
3333#include < algorithm>
3434
35+ #ifdef LIBFREENECT2_WITH_PROFILING
36+ #include < vector>
37+ #include < numeric>
38+ #include < functional>
39+ #include < cmath>
40+ #endif
41+
3542#ifdef LIBFREENECT2_WITH_CXX11_SUPPORT
3643#include < chrono>
3744#endif
@@ -192,6 +199,9 @@ class Timer
192199
193200 Timer ()
194201 {
202+ #if defined(LIBFREENECT2_WITH_OPENGL_SUPPORT)
203+ glfwInit ();
204+ #endif
195205 reset ();
196206 }
197207
@@ -209,10 +219,13 @@ class Timer
209219 time_start = std::chrono::high_resolution_clock::now ();
210220 }
211221
212- void stop ()
222+ double stop ()
213223 {
214- duration += std::chrono::duration_cast<std::chrono::duration<double >>(std::chrono::high_resolution_clock::now () - time_start).count ();
224+ auto time_diff = std::chrono::high_resolution_clock::now () - time_start;
225+ double this_duration = std::chrono::duration_cast<std::chrono::duration<double >>(time_diff).count ();
226+ duration += this_duration;
215227 count++;
228+ return this_duration;
216229 }
217230#elif defined(LIBFREENECT2_WITH_OPENGL_SUPPORT)
218231 double time_start;
@@ -222,28 +235,68 @@ class Timer
222235 time_start = glfwGetTime ();
223236 }
224237
225- void stop ()
238+ double stop ()
226239 {
227- duration += glfwGetTime () - time_start;
240+ double this_duration = glfwGetTime () - time_start;
241+ duration += this_duration;
228242 count++;
243+ return this_duration;
229244 }
230245#else
231246 void start ()
232247 {
233248 }
234249
235- void stop ()
250+ double stop ()
236251 {
252+ return 0 ;
237253 }
238254#endif
239255};
240256
241257class WithPerfLoggingImpl : public Timer
242258{
243259public:
260+ #ifdef LIBFREENECT2_WITH_PROFILING
261+ std::vector<double > stats;
262+ std::string name;
263+
264+ WithPerfLoggingImpl ()
265+ {
266+ stats.reserve (30 *100 );
267+ }
268+
269+ ~WithPerfLoggingImpl ()
270+ {
271+ if (stats.size () < 2 )
272+ return ;
273+ std::vector<double > &v = stats;
274+ std::sort (v.begin (), v.end ());
275+ double sum = std::accumulate (v.begin (), v.end (), 0.0 );
276+ size_t n = v.size ();
277+ double mean = sum / n;
278+ std::vector<double > diff (n);
279+ std::transform (v.begin (), v.end (), diff.begin (), std::bind2nd (std::minus<double >(), mean));
280+ double sqsum = std::inner_product (diff.begin (), diff.end (), diff.begin (), 0.0 );
281+ double std = std::sqrt (sqsum / (n-1 ));
282+
283+ std::cout << name << v[0 ] << " " << v[n/20 ] << " " << v[n/2 ] << " " << v[n - (n+19 )/20 ] << " " << v[n-1 ] << " mean=" << mean << " std=" << std << " n=" << n << std::endl;
284+ }
285+ #endif
286+
244287 std::ostream &stop (std::ostream &stream)
245288 {
289+ #ifndef LIBFREENECT2_WITH_PROFILING
246290 Timer::stop ();
291+ #else
292+ double this_duration = Timer::stop ();
293+ if (name.empty ())
294+ {
295+ std::stringstream &ss = static_cast <std::stringstream &>(stream);
296+ name = ss.str ();
297+ }
298+ stats.push_back (this_duration*1e3 );
299+ #endif
247300 if (count < 100 )
248301 {
249302 stream.setstate (std::ios::eofbit);
0 commit comments