hpp-util  4.11.0
Debugging tools for the HPP project.
timer.hh
Go to the documentation of this file.
1 // Copyright (c) 2015, LAAS-CNRS
2 // Authors: Thomas Moulard, Joseph Mirabel
3 //
4 // This file is part of hpp-util.
5 // hpp-util is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // hpp-util is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with hpp-util. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef HPP_UTIL_TIMER_HH
19 # define HPP_UTIL_TIMER_HH
20 
21 # include <chrono>
22 
23 # include <hpp/util/config.hh>
24 # include <hpp/util/debug.hh>
25 
26 namespace hpp
27 {
28  namespace debug
29  {
31  {
32  public:
33  typedef std::chrono::high_resolution_clock clock_type;
34  typedef clock_type::time_point time_point;
35  typedef std::chrono::duration<double> duration_type;
36 
37  explicit Timer (bool autoStart = false);
38  Timer (const Timer&);
39  Timer& operator= (const Timer&);
40  ~Timer ();
41 
42  const time_point& start ();
43  const time_point& stop ();
44  double duration () const;
45 
46  const time_point& getStart () const;
47  const time_point& getStop () const;
48 
49  std::ostream& print (std::ostream&) const;
50  private:
51  time_point start_;
52  time_point end_;
53  };
54 
55 # ifdef HPP_ENABLE_BENCHMARK
56 
57 # define hppStartBenchmark(ID) \
58  hppDout (benchmark, #ID << ": start"); \
59  ::hpp::debug::Timer _##ID##_timer_ (true)
60 
61 # define hppStopBenchmark(ID) \
62  do { \
63  _##ID##_timer_.stop (); \
64  hppDout (benchmark, #ID << ": stop"); \
65  } while (0)
66 
67 # define hppDisplayBenchmark(ID) \
68  hppDout (benchmark, #ID << ": "<< _##ID##_timer_.duration ());
69 
70 # define hppBenchmark(data) \
71  do { \
72  using namespace hpp; \
73  using namespace ::hpp::debug; \
74  std::stringstream __ss; \
75  __ss << data << iendl; \
76  logging.benchmark.write (__FILE__, __LINE__, __PRETTY_FUNCTION__, \
77  __ss); \
78  } while (0)
79 
80 # else
81 # define hppStartBenchmark(ID)
82 # define hppStopBenchmark(ID)
83 # define hppDisplayBenchmark(ID)
84 # define hppBenchmark(data)
85 # endif // HPP_ENABLE_BENCHMARK
86 
89  {
90  public:
91  struct Scope {
92  Scope (TimeCounter& t) : tc(t) { t.start(); }
93  ~Scope () { tc.stop(); }
94 
96  };
97 
98  typedef std::chrono::high_resolution_clock clock_type;
99  typedef clock_type::time_point time_point;
100  typedef std::chrono::duration<double> duration_type;
101 
102  TimeCounter (const std::string& name);
103 
104  void start ();
105  double stop ();
106  double last ();
107  void reset ();
108 
109  double min () const;
110  double max () const;
111  double mean () const;
112  double totalTime () const;
113 
114  std::ostream& print (std::ostream& os) const;
115 
116  private:
117  std::string n_;
118  unsigned long c_;
119  duration_type t_, last_, min_, max_;
120  time_point s_;
121  };
122 
123  std::ostream& operator<< (std::ostream& os, const TimeCounter& tc);
124 
125 # ifdef HPP_ENABLE_BENCHMARK
126 
129 
131 # define HPP_DEFINE_TIMECOUNTER(name) \
132  ::hpp::debug::TimeCounter _##name##_timecounter_ (#name)
133 # define HPP_SCOPE_TIMECOUNTER(name) \
135  ::hpp::debug::TimeCounter::Scope _##name##_scopetimecounter_ \
136  (_##name##_timecounter_)
137 # define HPP_START_TIMECOUNTER(name) \
139  _##name##_timecounter_.start ()
140 # define HPP_STOP_TIMECOUNTER(name) \
142  _##name##_timecounter_.stop()
143 # define HPP_DISPLAY_LAST_TIMECOUNTER(name) \
145  do { \
146  using namespace hpp; \
147  using namespace ::hpp::debug; \
148  std::stringstream __ss; \
149  __ss << #name << " last: " \
150  << _##name##_timecounter_.last() << iendl; \
151  logging.benchmark.write (__FILE__, __LINE__, \
152  __PRETTY_FUNCTION__, __ss); \
153  } while (0)
154 # define HPP_DISPLAY_TIMECOUNTER(name) \
156  do { \
157  using namespace hpp; \
158  using namespace ::hpp::debug; \
159  std::stringstream __ss; \
160  __ss << _##name##_timecounter_ << iendl; \
161  logging.benchmark.write (__FILE__, __LINE__, \
162  __PRETTY_FUNCTION__, __ss); \
163  } while (0)
164 # define HPP_RESET_TIMECOUNTER(name) \
166  _##name##_timecounter_.reset();
167 # define HPP_STREAM_TIMECOUNTER(os, name) \
169  os << _##name##_timecounter_
170 # else // HPP_ENABLE_BENCHMARK
172 # define HPP_DEFINE_TIMECOUNTER(name) \
173  struct _##name##_EndWithSemiColon_{}
174 # define HPP_SCOPE_TIMECOUNTER(name)
175 # define HPP_START_TIMECOUNTER(name)
176 # define HPP_STOP_TIMECOUNTER(name)
177 # define HPP_DISPLAY_LAST_TIMECOUNTER(name)
178 # define HPP_DISPLAY_TIMECOUNTER(name)
179 # define HPP_RESET_TIMECOUNTER(name)
180 # define HPP_STREAM_TIMECOUNTER(os, name) \
181  os
182 # endif // HPP_ENABLE_BENCHMARK
183 
184 # define HPP_STOP_AND_DISPLAY_TIMECOUNTER(name) \
185  HPP_STOP_TIMECOUNTER(name); \
186  HPP_DISPLAY_TIMECOUNTER(name)
187 
188  } // end of namespace debug
189 } // end of namespace hpp
190 
191 #endif // HPP_UTIL_TIMER_HH
~Scope()
Definition: timer.hh:93
std::chrono::high_resolution_clock clock_type
Definition: timer.hh:33
std::chrono::high_resolution_clock clock_type
Definition: timer.hh:98
clock_type::time_point time_point
Definition: timer.hh:34
Definition: assertion.hh:22
std::chrono::duration< double > duration_type
Definition: timer.hh:35
Definition: timer.hh:91
void start()
Definition: timer.cc:99
std::ostream & operator<<(std::ostream &os, const TimeCounter &tc)
Definition: timer.cc:158
Definition: timer.hh:30
Scope(TimeCounter &t)
Definition: timer.hh:92
#define HPP_UTIL_DLLAPI
Definition: config.hh:64
clock_type::time_point time_point
Definition: timer.hh:99
std::chrono::duration< double > duration_type
Definition: timer.hh:100
TimeCounter & tc
Definition: timer.hh:95
Computation of min, max and mean time from a set of measurements.
Definition: timer.hh:88