2 Copyright (C) 2012-2019 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 /** @file src/timer.cc
22 * @brief Some timing classes for debugging and profiling.
27 #include "compose.hpp"
34 using boost::optional;
36 /** @param n Name to use when giving output */
37 PeriodTimer::PeriodTimer (string n)
40 gettimeofday (&_start, 0);
43 /** Destroy PeriodTimer and output the time elapsed since its construction */
44 PeriodTimer::~PeriodTimer ()
47 gettimeofday (&stop, 0);
48 cout << N_("T: ") << _name << N_(": ") << (seconds (stop) - seconds (_start)) << N_("\n");
51 StateTimer::StateTimer (string n, string s)
60 StateTimer::StateTimer (string n)
67 StateTimer::set (string s)
73 StateTimer::set_internal (optional<string> s)
75 double const last = _time;
80 if (s && _counts.find(*s) == _counts.end()) {
81 _counts[*s] = Counts();
85 _counts[*_state].total_time += _time - last;
86 _counts[*_state].number++;
94 set_internal (optional<string>());
97 bool compare (pair<double, string> a, pair<double, string> b)
99 return a.first > b.first;
102 /** Destroy StateTimer and generate a summary of the state timings on cout */
103 StateTimer::~StateTimer ()
112 for (auto const& i: _counts) {
113 longest = max (longest, int(i.first.length()));
116 list<pair<double, string> > sorted;
118 for (auto const& i: _counts) {
119 string name = i.first + string(longest + 1 - i.first.size(), ' ');
121 snprintf (buffer, 64, "%.4f", i.second.total_time);
122 string total_time (buffer);
123 sorted.push_back (make_pair(i.second.total_time, String::compose("\t%1%2 %3 %4", name, total_time, i.second.number, (i.second.total_time / i.second.number))));
126 sorted.sort (compare);
128 cout << _name << N_(":\n");
129 for (auto const& i: sorted) {
130 cout << N_("\t") << i.second << "\n";