2 Copyright (C) 2012-2021 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/>.
23 * @brief Some timing classes for debugging and profiling.
27 #ifndef DCPOMATIC_TIMER_H
28 #define DCPOMATIC_TIMER_H
32 #include <boost/optional.hpp>
37 /** @class PeriodTimer
38 * @brief A class to allow timing of a period within the caller.
40 * On destruction, it will output the time since its construction.
45 explicit PeriodTimer (std::string n);
50 /** name to use when giving output */
52 /** time that this class was constructed */
53 struct timeval _start;
58 * @brief A class to allow measurement of the amount of time a program
59 * spends in one of a set of states.
61 * Once constructed, the caller can call set_state() whenever
62 * its state changes. When StateTimer is destroyed, it will
63 * output (to cout) a summary of the time spent in each state.
68 explicit StateTimer (std::string n);
69 /** @param n Name to use when giving output.
70 * @param s Initial state.
72 StateTimer (std::string n, std::string s);
75 /** @param s New state that the caller is in */
76 void set (std::string s);
79 std::string name () const {
86 double total_time = 0;
90 std::map<std::string, Counts> counts () const {
95 void set_internal (boost::optional<std::string> s);
97 /** name to add to the output */
100 boost::optional<std::string> _state;
101 /** time that _state was entered */
103 /** total time and number of entries for each state */
104 std::map<std::string, Counts> _counts;