libcvd
timer.h
1 //
3 // A timer class designed for dealing with timestamps
4 // CK Nov 2002
5 //
7 
8 #ifndef __CVD_TIMER_H
9 #define __CVD_TIMER_H
10 #include <algorithm>
11 #include <cassert>
12 #include <chrono>
13 #include <deque>
14 #include <iostream>
15 #include <string>
16 
17 namespace CVD
18 {
19 
26 class cvd_timer
27 {
28  public:
30  cvd_timer();
31 
33  double get_time();
34 
37  double conv_ntime(const double& time) const;
38 
40  double reset();
41 
42  private:
43  std::chrono::high_resolution_clock::time_point start;
44 };
45 
46 extern cvd_timer timer;
47 
50 double get_time_of_day();
51 
54 long long get_time_of_day_ns();
55 
61 {
62  public:
68  SimpleTimer(const std::string& description, const int& cycles_to_time = 1, bool output = true, std::ostream& out = std::cout)
69  : output_info(output)
70  , max(0)
71  , min(0)
72  , average(0)
73  , text(description)
74  , period(cycles_to_time)
75  , increment(0)
76  , timings(0)
77  , cumulative_time(0)
78  , time_at_start_of_timing(0)
79  , timing_started(false)
80  , sout(out)
81  {
82  assert(period > 0);
83  internal_cvd_timer = new cvd_timer();
84  }
85 
88  {
89  delete internal_cvd_timer;
90  }
91 
93  void click()
94  {
95  if(!timing_started)
96  {
97  timing_started = true;
98  time_at_start_of_timing = internal_cvd_timer->get_time();
99  }
100  else if(timing_started)
101  {
102  increment = internal_cvd_timer->get_time() - time_at_start_of_timing;
103  time_buffer.push_back(increment);
104  if((int)time_buffer.size() > period && period > 0)
105  time_buffer.pop_front();
106  timings++;
107  timing_started = false;
108  if(timings % period == 0 && output_info)
109  print();
110  }
111  }
112 
115  void print()
116  {
117  if(timings > 0)
118  {
119  if((int)time_buffer.size() == 1)
120  sout << text << " takes: " << get_average() << "s over 1 timing" << std::endl;
121  else
122  sout << text << " takes : av " << get_average() << "s , max " << get_max() << "s, min " << get_min() << "s, over " << time_buffer.size() << " timings" << std::endl;
123  }
124  else
125  sout << text << " section : error. No timed cycles. Use click() to start and stop timing." << std::endl;
126  }
127 
129  double get_max()
130  {
131  max = -1;
132  if(time_buffer.size() > 0)
133  max = time_buffer[0];
134  if(time_buffer.size() > 1)
135  for(int i = 0; i < (int)time_buffer.size(); ++i)
136  max = std::max(time_buffer[i], max);
137 
138  return max;
139  }
140 
142  double get_min()
143  {
144  min = -1;
145  if(time_buffer.size() > 0)
146  min = time_buffer[0];
147  if(time_buffer.size() > 1)
148  for(int i = 0; i < (int)time_buffer.size(); ++i)
149  min = std::min(time_buffer[i], min);
150  return min;
151  }
152 
154  double get_average()
155  {
156  average = -1;
157  if(time_buffer.size() > 0)
158  {
159  cumulative_time = 0;
160  for(int i = 0; i < (int)time_buffer.size(); ++i)
161  cumulative_time += time_buffer[i];
162  average = cumulative_time / static_cast<double>(time_buffer.size());
163  }
164  return average;
165  }
166 
167  private:
168  bool output_info;
169  double max, min, average;
170  std::string text;
171  int period;
172  double increment;
173  int timings;
174  double cumulative_time;
175  double time_at_start_of_timing;
176  bool timing_started;
177  cvd_timer* internal_cvd_timer;
178  std::ostream& sout;
179  std::deque<double> time_buffer;
180 };
181 
182 }
183 
184 #endif
double reset()
Sets the start time to the current time.
Definition: cvd_timer.cc:30
All classes and functions are within the CVD namespace.
Definition: argb.h:6
double conv_ntime(const double &time) const
Convert current time given as double by correcting for the start time.
Definition: cvd_timer.cc:50
~SimpleTimer()
Destructor. Deletes the internal cvd_timer.
Definition: timer.h:87
void print()
Output timing information (average, maximum and minimum times for a set of cycles).
Definition: timer.h:115
double get_average()
Calculate the average cycle time as double.
Definition: timer.h:154
cvd_timer()
Create a timer, and set the start time to be now.
Definition: cvd_timer.cc:25
double get_time_of_day()
Same as the system call gettimeofday, but returns seconds since the epoch as a double.
Definition: cvd_timer.cc:45
Provides a simple timer class which uses cvd_timer internally.
Definition: timer.h:60
double get_time()
How many seconds have elapsed since the start time?
Definition: cvd_timer.cc:39
long long get_time_of_day_ns()
Same as the system call gettimeofday, but returns nanoseconds seconds since the epoch.
Definition: cvd_timer.cc:18
double get_min()
Calculate the min cycle time as double.
Definition: timer.h:142
SimpleTimer(const std::string &description, const int &cycles_to_time=1, bool output=true, std::ostream &out=std::cout)
Create a simple timer object.
Definition: timer.h:68
void click()
Begin or end a timing cycle. Automatically calls print() when cycles_to_time cycles are reached...
Definition: timer.h:93
Provides the time elapsed in seconds.
Definition: timer.h:26
double get_max()
Calculate the max cycle time as double.
Definition: timer.h:129