X-Boost  2.3.8
timer.h
Go to the documentation of this file.
1 /* XBoost: Ada-Boost and Friends on Haar/ICF/HOG Features, Library and ToolBox
2  *
3  * Copyright (c) 2008-2014 Paolo Medici <medici@ce.unipr.it>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef _H_TIMER_
22 #define _H_TIMER_
23 
28 #include <iosfwd>
29 
30 #ifdef WIN32
31 
32 class Counter {
33  double time_factor; // Time Scaling Factor
34 public:
35  Counter() {
36  LONGLONG perf_cnt;
37 
38  if (QueryPerformanceFrequency((LARGE_INTEGER *) &perf_cnt))
39  {
40  time_factor=1.0/perf_cnt;
41  }
42  }
43 
44  inline double GetTime()
45  {
46  LONGLONG time;
47 
48  QueryPerformanceCounter((LARGE_INTEGER *) &time);
49  return (time * time_factor);
50  }
51 };
52 
53 #else
54 
55 #include <sys/time.h>
56 #include <time.h>
57 
58 class Counter {
59  //double time_factor; // Time Scaling Factor
60  /*inline unsigned long long rdtsc()
61  {
62  unsigned long long v;
63 
64  __asm__ ("rdtsc" : "=A" (v));
65 
66  return v;
67  }*/
68 public:
69  Counter()
70  {
71  }
72 
73  inline double GetTime() const
74  {
75  struct timespec ts;
76  clock_gettime(CLOCK_REALTIME, &ts);
77  return (ts.tv_sec*1.0 + ts.tv_nsec * 0.000000001);
78  }
79 };
80 
81 
82 #endif
83 
84 class Timer: public Counter {
85  double user_time;
86 public:
87  Timer() : Counter()
88  {
89  user_time = Counter::GetTime();
90  }
91 
92  inline void Start()
93  {
94  user_time = Counter::GetTime();
95  }
96 
97  inline double GetTime() const
98  {
99  return Counter::GetTime() - user_time;
100  }
101 
102 };
103 
105 class Statistic: public Counter {
106  int count;
107  double min_time, max_time, sum_time, sum2_time;
108  double start_time;
109  double last;
110  double tmp;
111  public:
112  void Reset() { count = 0; min_time = max_time = sum2_time = sum_time = 0.0; }
113  Statistic() { Reset(); }
114 
115  void Start()
116  {
117  start_time = Counter::GetTime();
118  }
119 
120  void Pause()
121  {
122  tmp = Counter::GetTime() - start_time;
123  }
124 
125  void Resume()
126  {
127  start_time = Counter::GetTime() - tmp;
128  }
129 
130  void Stop()
131  {
132  last = Counter::GetTime() - start_time;
133  sum_time += last;
134  sum2_time += last*last;
135  if(count == 0)
136  {
137  min_time = max_time = last;
138  }
139  else
140  {
141  if(last<min_time)
142  min_time = last;
143  if(last>max_time)
144  max_time = last;
145  }
146  count ++;
147  }
148 
149  double Last() const { return last; }
150  double Avg() const { return sum_time / count; }
151  double Var() const { return sum2_time / count - (sum_time / count) * (sum_time / count); }
152  double Min() const { return min_time; }
153  double Max() const { return max_time; }
154  int Count() const { return count; }
155 
156  operator bool() const { return count>0; }
157 
158 };
159 
160 std::ostream & operator << (std::ostream &, const Statistic &);
161 
162 #endif
Definition: timer.h:84
Definition: timer.h:58
Compute timing statistics.
Definition: timer.h:105