| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 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 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class cvd_timer |
| 27 |
{ |
| 28 |
public: |
| 29 |
|
| 30 |
cvd_timer(); |
| 31 |
|
| 32 |
|
| 33 |
double get_time(); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
double conv_ntime(const double& time) const; |
| 38 |
|
| 39 |
|
| 40 |
double reset(); |
| 41 |
|
| 42 |
private: |
| 43 |
std::chrono::high_resolution_clock::time_point start; |
| 44 |
}; |
| 45 |
|
| 46 |
extern cvd_timer timer; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
double get_time_of_day(); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
long long get_time_of_day_ns(); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
class SimpleTimer |
| 61 |
{ |
| 62 |
public: |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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 |
|
| 87 |
~SimpleTimer() |
| 88 |
{ |
| 89 |
delete internal_cvd_timer; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 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 |
|
| 114 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |