2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/timer.cc
21 * @brief Some timing classes for debugging and profiling.
33 /** @param n Name to use when giving output */
34 PeriodTimer::PeriodTimer (string n)
37 gettimeofday (&_start, 0);
40 /** Destroy PeriodTimer and output the time elapsed since its construction */
41 PeriodTimer::~PeriodTimer ()
44 gettimeofday (&stop, 0);
45 cout << N_("T: ") << _name << N_(": ") << (seconds (stop) - seconds (_start)) << N_("\n");
48 /** @param n Name to use when giving output.
49 * @param s Initial state.
51 StateTimer::StateTimer (string n, string s)
60 /** @param s New state that the caller is in */
62 StateTimer::set_state (string s)
64 double const last = _time;
69 if (_totals.find (s) == _totals.end ()) {
73 _totals[_state] += _time - last;
77 /** Destroy StateTimer and generate a summary of the state timings on cout */
78 StateTimer::~StateTimer ()
80 if (_state.empty ()) {
87 cout << _name << N_(":\n");
88 for (map<string, double>::iterator i = _totals.begin(); i != _totals.end(); ++i) {
89 cout << N_("\t") << i->first << " " << i->second << N_("\n");