1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/*$Id: l_timer.cc,v 26.81 2008/05/27 05:34:00 al Exp $ -*- C++ -*-
* Copyright (C) 2001 Albert Davis
* Author: Albert Davis <aldavis@gnu.org>
*
* This file is part of "Gnucap", the Gnu Circuit Analysis Package
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*------------------------------------------------------------------
* Time a command, or whatever
*/
//testing=script,sparse 2006.07.13
#include "l_timer.h"
/*--------------------------------------------------------------------------*/
// TIMER::TIMER();
// TIMER::TIMER(const char*);
// TIMER& TIMER::fullreset();
// TIMER& TIMER::reset();
// TIMER& TIMER::start();
// TIMER& TIMER::stop();
// TIMER& TIMER::check();
// TIMER& TIMER::print();
// TIMER& TIMER::operator=(const TIMER&);
TIMER operator-(const TIMER&,const TIMER&);
/*--------------------------------------------------------------------------*/
inline double run_time()
{
return static_cast<double>(clock()) / static_cast<double>(CLOCKS_PER_SEC);
}
/*--------------------------------------------------------------------------*/
TIMER::TIMER()
{
fullreset();
}
/*--------------------------------------------------------------------------*/
TIMER::TIMER(const std::string& label)
:_name(label)
{
fullreset();
}
/*--------------------------------------------------------------------------*/
TIMER& TIMER::fullreset()
{
_total = 0.;
return reset();
}
/*--------------------------------------------------------------------------*/
TIMER& TIMER::reset()
{
_last = 0.;
_ref = 0.;
_running = false;
return *this;
}
/*--------------------------------------------------------------------------*/
TIMER& TIMER::start()
{
assert(!_running);
if (_running) {
untested();
stop();
}
_ref = run_time();
_running = true;
return *this;
}
/*--------------------------------------------------------------------------*/
TIMER& TIMER::stop()
{
if (_running) {
double runtime = run_time() - _ref;
_ref = 0.;
_last += runtime;
_total += runtime;
_running = false;
}
return *this;
}
/*--------------------------------------------------------------------------*/
TIMER& TIMER::check()
{
itested();
if (_running) {
itested();
stop();
start();
}else{
untested();
}
return *this;
}
/*--------------------------------------------------------------------------*/
TIMER& TIMER::print(OMSTREAM& s)
{
s.form("%10s %8.2f %8.2f\n", _name.c_str(), _last, _total);
return *this;
}
/*--------------------------------------------------------------------------*/
TIMER& TIMER::operator=(const TIMER& x)
{
_last = x._last;
_ref = x._ref;
_total = x._total;
_running = x._running;
// but don't copy the name
return *this;
}
/*--------------------------------------------------------------------------*/
TIMER operator-(const TIMER& x, const TIMER& y)
{
TIMER z("temp");
z._last = x._last - y._last;
z._ref = 0.; // when did the difference start running?
z._total = x._total - y._total;
z._running = false;
// but don't copy the name
return z;
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// vim:ts=8:sw=2:noet:
|