Debian Codegraph @

cvd/timer.h

raw symbols SCIP
1 ////////////////////////////////////////////////////////
2 //
3 // A timer class designed for dealing with timestamps
4 // CK Nov 2002
5 //
6 ////////////////////////////////////////////////////////
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
20 /// Provides the time elapsed in seconds. This predominantly a wrapper for
21 /// the system call
22 /// gettimeofday(), but can also provides a convenient way of converting from other
23 /// time units. In all cases, the time is given relative to the time the class
24 /// was created, or the last time reset() was called.
25 /// @ingroup gCPP
26 class cvd_timer
27 {
28 public:
29 /// Create a timer, and set the start time to be now
30 cvd_timer();
31
32 /// How many seconds have elapsed since the start time?
33 double get_time();
34
35 /// Convert current time given as double by correcting for the start time
36 /// @param time current time as double
37 double conv_ntime(const double& time) const;
38
39 /// Sets the start time to the current time
40 double reset();
41
42 private:
43 std::chrono::high_resolution_clock::time_point start;
44 };
45
46 extern cvd_timer timer;
47
48 /// Same as the system call gettimeofday, but returns seconds since
49 /// the epoch as a double.
50 double get_time_of_day();
51
52 /// Same as the system call gettimeofday, but returns nanoseconds seconds since
53 /// the epoch
54 long long get_time_of_day_ns();
55
56 /// Provides a simple timer class which uses cvd_timer internally.
57 /// Statistics (average, max and min cycle time) are kept and output for a number of timing cycles.
58 /// Cycle times are stored in a std::deque<double> and statistics only calculated when the get or print functions are called.
59 /// @ingroup gCPP
60 class SimpleTimer
61 {
62 public:
63 /// Create a simple timer object.
64 /// @param description A string description of what is being timed (used in output)
65 /// @param cycles_to_time How many cycles to time before the times are averaged and output (default 1).
66 /// @param output Whether or not to output timing information. Default is true.
67 /// @param out std::ostream to send output information to. Default std::cout.
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
86 /// Destructor. Deletes the internal cvd_timer
87 ~SimpleTimer()
88 {
89 delete internal_cvd_timer;
90 }
91
92 /// Begin or end a timing cycle. Automatically calls print() when cycles_to_time cycles are reached.
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
113 /// Output timing information (average, maximum and minimum times for a set of cycles).
114 /// Automatically called after cycles_to_time cycles but can be called manually.
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
128 /// Calculate the max cycle time as double
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
141 /// Calculate the min cycle time as double
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
153 /// Calculate the average cycle time as double
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