00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00022
00023
00024
00025
00027
00028 #ifndef __CVD_TIMER_H
00029 #define __CVD_TIMER_H
00030 #include <iostream>
00031 #include <string>
00032 #include <cassert>
00033 #include <deque>
00034 struct timeval;
00035
00036 namespace CVD {
00037
00044 class cvd_timer
00045 {
00046 public:
00048 cvd_timer();
00050 double get_time();
00051
00055 double conv_ntime(signed long long time);
00056
00060 double conv_ntime(const struct timeval& tv);
00061
00064 double conv_ntime(const double time) const {
00065 return time - startTime * 1.e-6;
00066 }
00067
00069 double reset();
00070
00071
00072 private:
00073 unsigned long long startTime;
00074 };
00075
00078 extern cvd_timer timer;
00079
00082 double get_time_of_day();
00083
00084
00089 class SimpleTimer
00090 {
00091 public:
00097 SimpleTimer(const std::string &description, const int &cycles_to_time=1, bool output=true, std::ostream &out=std::cout) : output_info(output), max(0), min(0), average(0), text(description), period(cycles_to_time), increment(0), timings(0), cumulative_time(0), time_at_start_of_timing(0), timing_started(false), sout(out)
00098 {
00099 assert(period>0);
00100 internal_cvd_timer=new cvd_timer();
00101 }
00102
00104 ~SimpleTimer()
00105 {
00106 delete internal_cvd_timer;
00107 }
00108
00110 void click()
00111 {
00112 if (!timing_started)
00113 {
00114 timing_started=true;
00115 time_at_start_of_timing=internal_cvd_timer->get_time();
00116 }
00117 else if (timing_started)
00118 {
00119 increment=internal_cvd_timer->get_time()-time_at_start_of_timing;
00120 time_buffer.push_back(increment);
00121 if((int)time_buffer.size()>period&&period>0)
00122 time_buffer.pop_front();
00123 timings++;
00124 timing_started=false;
00125 if (timings%period==0 && output_info)
00126 print();
00127 }
00128 }
00129
00132 void print()
00133 {
00134 if(timings>0)
00135 {
00136 if((int)time_buffer.size()==1)
00137 sout<<text<<" takes: "<<get_average()<<"s over 1 timing"<<std::endl;
00138 else
00139 sout<<text<<" takes : av "<<get_average()<<"s , max "<<get_max()<<"s, min "<<get_min()<<"s, over "<<time_buffer.size()<<" timings"<<std::endl;
00140 }
00141 else
00142 sout<<text<<" section : error. No timed cycles. Use click() to start and stop timing."<<std::endl;
00143 }
00144
00146 double get_max(){
00147 max=-1;
00148 if (time_buffer.size()>0)
00149 max=time_buffer[0];
00150 if(time_buffer.size()>1)
00151 for(int i=0; i<(int)time_buffer.size(); ++i)
00152 max=std::max(time_buffer[i], max);
00153
00154 return max;
00155
00156 }
00157
00159 double get_min(){
00160 min=-1;
00161 if (time_buffer.size()>0)
00162 min=time_buffer[0];
00163 if(time_buffer.size()>1)
00164 for(int i=0; i<(int)time_buffer.size(); ++i)
00165 min=std::min(time_buffer[i], min);
00166 return min;
00167 }
00168
00170 double get_average(){
00171 average=-1;
00172 if(time_buffer.size()>0){
00173 cumulative_time=0;
00174 for(int i=0; i<(int)time_buffer.size(); ++i)
00175 cumulative_time+=time_buffer[i];
00176 average=cumulative_time/time_buffer.size();
00177 }
00178 return average;
00179 }
00180
00181 private:
00182 bool output_info;
00183 double max, min, average;
00184 std::string text;
00185 int period;
00186 double increment;
00187 int timings;
00188 double cumulative_time;
00189 double time_at_start_of_timing;
00190 bool timing_started;
00191 cvd_timer* internal_cvd_timer;
00192 std::ostream& sout;
00193 std::deque<double> time_buffer;
00194
00195
00196 };
00197
00198 }
00199
00200 #endif