hpp-util  4.9.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 "boost/date_time/posix_time/posix_time_types.hpp"
22 
23 # ifdef HPP_ENABLE_BENCHMARK
24 # include <boost/date_time/posix_time/posix_time.hpp>
25 # endif // HPP_ENABLE_BENCHMARK
26 
27 # include <hpp/util/config.hh>
28 # include <hpp/util/debug.hh>
29 
30 namespace hpp
31 {
32  namespace debug
33  {
34  class HPP_UTIL_DLLAPI Timer
35  {
36  public:
37  typedef boost::posix_time::ptime ptime;
38  typedef boost::posix_time::time_duration time_duration;
39  typedef boost::posix_time::time_period time_period;
40 
41  explicit Timer (bool autoStart = false);
42  Timer (const Timer&);
43  Timer& operator= (const Timer&);
44  ~Timer ();
45 
46  const ptime& start ();
47  const ptime& stop ();
48  time_duration duration () const;
49 
50  const ptime& getStart () const;
51  const ptime& getStop () const;
52 
53  std::ostream& print (std::ostream&) const;
54  private:
55  ptime start_;
56  ptime end_;
57  };
58 
59 # ifdef HPP_ENABLE_BENCHMARK
60 
61 # define hppStartBenchmark(ID) \
62  hppDout (benchmark, #ID << ": start"); \
63  ::hpp::debug::Timer _##ID##_timer_ (true)
64 
65 # define hppStopBenchmark(ID) \
66  do { \
67  _##ID##_timer_.stop (); \
68  hppDout (benchmark, #ID << ": stop"); \
69  } while (0)
70 
71 # define hppDisplayBenchmark(ID) \
72  hppDout (benchmark, #ID << ": "<< _##ID##_timer_.duration ());
73 
74 # define hppBenchmark(data) \
75  do { \
76  using namespace hpp; \
77  using namespace ::hpp::debug; \
78  std::stringstream __ss; \
79  __ss << data << iendl; \
80  logging.benchmark.write (__FILE__, __LINE__, __PRETTY_FUNCTION__, \
81  __ss); \
82  } while (0)
83 
84 # else
85 # define hppStartBenchmark(ID)
86 # define hppStopBenchmark(ID)
87 # define hppDisplayBenchmark(ID)
88 # define hppBenchmark(data)
89 # endif // HPP_ENABLE_BENCHMARK
90 
92  class HPP_UTIL_DLLAPI TimeCounter
93  {
94  public:
95  struct Scope {
96  Scope (TimeCounter& t) : tc(t) { t.start(); }
97  ~Scope () { tc.stop(); }
98 
100  };
101 
102  typedef boost::posix_time::ptime ptime;
103  typedef boost::posix_time::time_duration time_duration;
104 
105  TimeCounter (const std::string& name);
106 
107  void start ();
108  time_duration stop ();
109  time_duration last ();
110  void reset ();
111 
112  time_duration min () const;
113  time_duration max () const;
114  time_duration mean () const;
115  time_duration totalTime () const;
116 
117  std::ostream& print (std::ostream& os) const;
118 
119  private:
120  std::string n_;
121  unsigned long c_;
122  time_duration t_, last_, min_, max_;
123  ptime s_;
124  };
125 
126  std::ostream& operator<< (std::ostream& os, const TimeCounter& tc);
127 
128 # ifdef HPP_ENABLE_BENCHMARK
129 
132 
134 # define HPP_DEFINE_TIMECOUNTER(name) \
135  ::hpp::debug::TimeCounter _##name##_timecounter_ (#name)
136 # define HPP_SCOPE_TIMECOUNTER(name) \
138  ::hpp::debug::TimeCounter::Scope _##name##_scopetimecounter_ \
139  (_##name##_timecounter_)
140 # define HPP_START_TIMECOUNTER(name) \
142  _##name##_timecounter_.start ()
143 # define HPP_STOP_TIMECOUNTER(name) \
145  _##name##_timecounter_.stop()
146 # define HPP_DISPLAY_LAST_TIMECOUNTER(name) \
148  do { \
149  using namespace hpp; \
150  using namespace ::hpp::debug; \
151  std::stringstream __ss; \
152  __ss << #name << " last: " \
153  << _##name##_timecounter_.last() << iendl; \
154  logging.benchmark.write (__FILE__, __LINE__, \
155  __PRETTY_FUNCTION__, __ss); \
156  } while (0)
157 # define HPP_DISPLAY_TIMECOUNTER(name) \
159  do { \
160  using namespace hpp; \
161  using namespace ::hpp::debug; \
162  std::stringstream __ss; \
163  __ss << _##name##_timecounter_ << iendl; \
164  logging.benchmark.write (__FILE__, __LINE__, \
165  __PRETTY_FUNCTION__, __ss); \
166  } while (0)
167 # define HPP_RESET_TIMECOUNTER(name) \
169  _##name##_timecounter_.reset();
170 # define HPP_STREAM_TIMECOUNTER(os, name) \
172  os << _##name##_timecounter_
173 # else // HPP_ENABLE_BENCHMARK
175 # define HPP_DEFINE_TIMECOUNTER(name) \
176  struct _##name##_EndWithSemiColon_{}
177 # define HPP_SCOPE_TIMECOUNTER(name)
178 # define HPP_START_TIMECOUNTER(name)
179 # define HPP_STOP_TIMECOUNTER(name)
180 # define HPP_DISPLAY_LAST_TIMECOUNTER(name)
181 # define HPP_DISPLAY_TIMECOUNTER(name)
182 # define HPP_RESET_TIMECOUNTER(name)
183 # define HPP_STREAM_TIMECOUNTER(os, name) \
184  os
185 # endif // HPP_ENABLE_BENCHMARK
186 
187 # define HPP_STOP_AND_DISPLAY_TIMECOUNTER(name) \
188  HPP_STOP_TIMECOUNTER(name); \
189  HPP_DISPLAY_TIMECOUNTER(name)
190 
191  } // end of namespace debug
192 } // end of namespace hpp
193 
194 #endif // HPP_UTIL_TIMER_HH
boost::posix_time::time_duration time_duration
Definition: timer.hh:38
~Scope()
Definition: timer.hh:97
Definition: assertion.hh:24
Definition: timer.hh:95
void start()
Definition: timer.cc:101
boost::posix_time::time_period time_period
Definition: timer.hh:39
std::ostream & operator<<(std::ostream &os, const TimeCounter &tc)
Definition: timer.cc:160
Definition: timer.hh:34
Scope(TimeCounter &t)
Definition: timer.hh:96
boost::posix_time::time_duration time_duration
Definition: timer.hh:103
boost::posix_time::ptime ptime
Definition: timer.hh:37
TimeCounter & tc
Definition: timer.hh:99
boost::posix_time::ptime ptime
Definition: timer.hh:102
Computation of min, max and mean time from a set of measurements.
Definition: timer.hh:92