00001
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "CTB.hxx"
00015 #include "CTBtraceClock.hxx"
00016
00017 #include "CTBsystem.hxx"
00018 #include "CTBprintf.hxx"
00019
00074
00076
00077 CTBtraceClock::CTBtraceClock()
00078 {
00079 Init(0,false,false);
00080 }
00081
00082
00084
00089 CTBtraceClock::CTBtraceClock(const char* c_text, bool b_start, bool b_print)
00090 {
00091 Init(c_text,b_start,b_print);
00092 }
00093
00094
00096
00101 CTBtraceClock::~CTBtraceClock()
00102 {
00103 if (mb_autoprint) ToStream();
00104 }
00105
00106
00108
00109 void CTBtraceClock::Start()
00110 {
00111 if (!mb_started) {
00112 md_cpustart = CTBsystem::CpuTime();
00113 md_elastart = CTBsystem::ElapsedTime();
00114 mi_intervals += 1;
00115 mb_started = true;
00116 }
00117 }
00118
00119
00121
00122 void CTBtraceClock::Stop()
00123 {
00124 if (mb_started) {
00125 Update(e_cputime|e_elatime);
00126 mb_started = false;
00127 }
00128 }
00129
00130
00132
00133 void CTBtraceClock::Reset()
00134 {
00135 md_cputime = 0.;
00136 md_elatime = 0.;
00137 mi_intervals = 0;
00138 if (mb_started) {
00139 md_cpustart = CTBsystem::CpuTime();
00140 md_elastart = CTBsystem::ElapsedTime();
00141 mi_intervals += 1;
00142 }
00143 }
00144
00145
00147
00158 void CTBtraceClock::ToStream(ostream& os)
00159 {
00160 Update(e_cputime|e_elatime);
00161
00162 os << CTBprintf(mc_text,"-s",30) << " CPU ";
00163 if (mi_intervals > 0) os << CTBprintf(md_cputime,"f",9,3);
00164 else os << " -.---";
00165 os << "s elapsed ";
00166 if (mi_intervals > 0) os << CTBprintf(md_elatime,"f",9,3);
00167 else os << " -.--";
00168 os << "s";
00169 if (md_elatime > 0.)
00170 os << " (" << CTBprintf(100.*md_cputime/md_elatime,"f",6,2) << "%)";
00171 os << endl;
00172 }
00173
00174
00176
00177 void CTBtraceClock::Init(const char* c_text, bool b_start, bool b_print)
00178 {
00179 mc_text = c_text ? c_text : "";
00180 mb_started = false;
00181 mb_autoprint = b_print;
00182 md_cputime = 0.;
00183 md_elatime = 0.;
00184 mi_intervals = 0;
00185 if (b_start) Start();
00186 }
00187
00188
00190
00191 void CTBtraceClock::Update(int i_flags)
00192 {
00193 if (mb_started) {
00194 if (i_flags & e_cputime) {
00195 double d_cpustop = CTBsystem::CpuTime();
00196 double d_dtime = d_cpustop - md_cpustart;
00197
00198 if (d_dtime > 1.e-6) {
00199 md_cputime += d_dtime;
00200 md_cpustart = d_cpustop;
00201 }
00202 }
00203 if (i_flags & e_elatime) {
00204 double d_elastop = CTBsystem::ElapsedTime();
00205 double d_dtime = d_elastop - md_elastart;
00206
00207 if (d_dtime > 1.e-6) {
00208 md_elatime += d_dtime;
00209 md_elastart = d_elastop;
00210 }
00211 }
00212 }
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 #if (defined(CTB__OutLine) || defined(CTBtraceClock__OutLine))
00228 #define inline
00229 #include "CTBtraceClock.icc"
00230 #undef inline
00231 #endif