STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
timer.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/timer.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002, 2005 Roman Dementiev <[email protected]>
7  * Copyright (C) 2007-2009 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2008 Johannes Singler <[email protected]>
9  * Copyright (C) 2013-2014 Timo Bingmann <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
16 #ifndef STXXL_COMMON_TIMER_HEADER
17 #define STXXL_COMMON_TIMER_HEADER
18 
19 #include <stxxl/bits/config.h>
20 #include <stxxl/bits/namespace.h>
21 #include <stxxl/bits/verbose.h>
23 
24 #if STXXL_BOOST_TIMESTAMP
25  #include <boost/date_time/posix_time/posix_time.hpp>
26  #include <cmath>
27 #elif STXXL_WINDOWS
28  #ifndef NOMINMAX
29  #define NOMINMAX
30  #endif
31  #include <windows.h>
32 #else
33  #include <ctime>
34  #include <sys/time.h>
35 #endif
36 
38 
39 //! \addtogroup support
40 //! \{
41 
42 //! Returns number of seconds since the epoch, high resolution.
43 inline double
45 {
46 #if STXXL_BOOST_TIMESTAMP
47  boost::posix_time::ptime MyTime = boost::posix_time::microsec_clock::local_time();
48  boost::posix_time::time_duration Duration =
49  MyTime - boost::posix_time::time_from_string("1970-01-01 00:00:00.000");
50  double sec = double(Duration.hours()) * 3600. +
51  double(Duration.minutes()) * 60. +
52  double(Duration.seconds()) +
53  double(Duration.fractional_seconds()) / (pow(10., Duration.num_fractional_digits()));
54  return sec;
55 #elif STXXL_WINDOWS
56  return GetTickCount() / 1000.0;
57 #else
58  struct timeval tp;
59  gettimeofday(&tp, NULL);
60  return double(tp.tv_sec) + double(tp.tv_usec) / 1000000.;
61 #endif
62 }
63 
64 /*!
65  * Class timer is a simple stop watch timer. It uses the timestamp() function
66  * to get the current time when start() is called. Then, after some processing,
67  * the function stop() functions can be called, or seconds() and other
68  * accessors can be called directly.
69  */
70 class timer
71 {
72  //! boolean whether the stopwatch timer is currently running
73  bool running;
74 
75  //! total accumulated time in seconds.
76  double accumulated;
77 
78  //! last start time of the stopwatch
79  double last_clock;
80 
81  //! return current timestamp
82  static inline double timestamp()
83  {
84  return stxxl::timestamp();
85  }
86 
87 public:
88  //! boolean indicating that this class does real timing
89  static const bool is_real = true;
90 
91  //! initialize and optionally immediately start the timer
92  inline timer(bool start_immediately = false)
93  : running(false), accumulated(0), last_clock(0)
94  {
95  if (start_immediately) start();
96  }
97 
98  //! start timer
99  inline void start()
100  {
101  running = true;
102  last_clock = timestamp();
103  }
104 
105  //! stop timer
106  inline void stop()
107  {
108  running = false;
109  accumulated += timestamp() - last_clock;
110  }
111 
112  //! return accumulated time
113  inline void reset()
114  {
115  accumulated = 0.;
116  last_clock = timestamp();
117  }
118 
119  //! return currently accumulated time in milliseconds
120  inline double mseconds() const
121  {
122  if (running)
123  return (accumulated + timestamp() - last_clock) * 1000.;
124 
125  return (accumulated * 1000.);
126  }
127 
128  //! return currently accumulated time in microseconds
129  inline double useconds() const
130  {
131  if (running)
132  return (accumulated + timestamp() - last_clock) * 1000000.;
133 
134  return (accumulated * 1000000.);
135  }
136 
137  //! return currently accumulated time in seconds (as double)
138  inline double seconds() const
139  {
140  if (running)
141  return (accumulated + timestamp() - last_clock);
142 
143  return (accumulated);
144  }
145 
146  //! accumulate elapsed time from another timer
147  inline timer& operator += (const timer& tm)
148  {
149 #if STXXL_PARALLEL
150 #pragma omp atomic
151 #endif
152  accumulated += tm.seconds();
153  return *this;
154  }
155 
156  //! direct <<-operator for ostream. Can be used for printing with std::cout.
157  friend std::ostream& operator << (std::ostream& os, const timer& t)
158  {
159  return os << t.seconds() << 's';
160  }
161 };
162 
163 /*!
164  * Class fake_timer is a drop-in replacement for timer, which does
165  * nothing. Using the fake class, timers can quickly be disabled in release
166  * builds, but still be available for debugging session.
167  *
168  * \see timer
169  */
171 {
172 public:
173  //! boolean indicating that this class does NOT do real timing
174  static const bool is_real = false;
175 
176  //! initialize and optionally immediately start the timer
177  fake_timer(bool = false)
178  { }
179 
180  //! start timer
181  void start()
182  { }
183 
184  //! stop timer
185  void stop()
186  { }
187 
188  //! return accumulated time
189  void reset()
190  { }
191 
192  //! return currently accumulated time in milliseconds
193  double mseconds() const
194  {
195  return std::numeric_limits<double>::quiet_NaN();
196  }
197 
198  //! return currently accumulated time in microseconds
199  double useconds() const
200  {
201  return std::numeric_limits<double>::quiet_NaN();
202  }
203 
204  //! return currently accumulated time in seconds (as double)
205  double seconds() const
206  {
207  return std::numeric_limits<double>::quiet_NaN();
208  }
209 
210  //! accumulate elapsed time from another timer
212  {
213  return *this;
214  }
215 
216  //! direct <<-operator for ostream. Can be used for printing with std::cout.
217  friend std::ostream& operator << (std::ostream& os, const fake_timer& t)
218  {
219  return os << t.seconds() << 's';
220  }
221 };
222 
223 /*!
224  * Simple scoped timer, which takes a text message and prints the duration
225  * until the scope is destroyed.
226  */
228 {
229 protected:
230  //! message
231  std::string m_message;
232 
233  //! bytes processed
235 
236  //! timer
238 
239 public:
240  //! save message and start timer
241  scoped_print_timer(const std::string& message, const uint64 bytes = 0)
242  : m_message(message),
243  m_bytes(bytes),
244  m_timer(true)
245  {
246  STXXL_MSG("Starting " << message);
247  }
248 
249  //! on destruction: tell the time
251  {
252  if (m_bytes == 0) {
253  STXXL_MSG("Finished "
254  << m_message
255  << " after " << m_timer.seconds() << " seconds");
256  }
257  else {
258  double bps = (double)m_bytes / m_timer.seconds();
259 
260  STXXL_MSG("Finished "
261  << m_message
262  << " after " << m_timer.seconds() << " seconds. "
263  << "Processed " << format_IEC_size(m_bytes) << "B"
264  << " @ " << format_IEC_size((uint64)bps) << "B/s");
265  }
266  }
267 
268  //! constant access to enclosed timer
269  const stxxl::timer & timer() const
270  {
271  return m_timer;
272  }
273 };
274 
275 //! \}
276 
278 
279 #endif // !STXXL_COMMON_TIMER_HEADER
280 // vim: et:ts=4:sw=4
~scoped_print_timer()
on destruction: tell the time
Definition: timer.h:250
bool running
boolean whether the stopwatch timer is currently running
Definition: timer.h:73
friend std::ostream & operator<<(std::ostream &os, const uint_pair &a)
make a uint_pair outputtable via iostreams, using unsigned long long.
Definition: uint_types.h:228
double mseconds() const
return currently accumulated time in milliseconds
Definition: timer.h:193
std::string m_message
message
Definition: timer.h:231
fake_timer(bool=false)
initialize and optionally immediately start the timer
Definition: timer.h:177
unsigned long long int uint64
Definition: types.h:39
void start()
start timer
Definition: timer.h:99
Simple scoped timer, which takes a text message and prints the duration until the scope is destroyed...
Definition: timer.h:227
void stop()
stop timer
Definition: timer.h:106
double useconds() const
return currently accumulated time in microseconds
Definition: timer.h:199
Class fake_timer is a drop-in replacement for timer, which does nothing.
Definition: timer.h:170
void start()
start timer
Definition: timer.h:181
stxxl::timer m_timer
timer
Definition: timer.h:237
uint_pair & operator+=(const uint_pair &b)
addition operator (uses 64-bit arithmetic)
Definition: uint_types.h:183
void reset()
return accumulated time
Definition: timer.h:113
double seconds() const
return currently accumulated time in seconds (as double)
Definition: timer.h:205
void reset()
return accumulated time
Definition: timer.h:189
double seconds() const
return currently accumulated time in seconds (as double)
Definition: timer.h:138
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
timer(bool start_immediately=false)
initialize and optionally immediately start the timer
Definition: timer.h:92
void stop()
stop timer
Definition: timer.h:185
static double timestamp()
return current timestamp
Definition: timer.h:82
std::string format_IEC_size(uint64 number)
Format a byte size using IEC (Ki, Mi, Gi, Ti) suffixes (powers of two). Returns &quot;123 Ki&quot; or similar...
Definition: utils.cpp:111
const stxxl::timer & timer() const
constant access to enclosed timer
Definition: timer.h:269
double accumulated
total accumulated time in seconds.
Definition: timer.h:76
double last_clock
last start time of the stopwatch
Definition: timer.h:79
double useconds() const
return currently accumulated time in microseconds
Definition: timer.h:129
static const size_t bytes
number of bytes in uint_pair
Definition: uint_types.h:96
double timestamp()
Returns number of seconds since the epoch, high resolution.
Definition: timer.h:44
#define STXXL_MSG(x)
Definition: verbose.h:73
scoped_print_timer(const std::string &message, const uint64 bytes=0)
save message and start timer
Definition: timer.h:241
Class timer is a simple stop watch timer.
Definition: timer.h:70
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
double mseconds() const
return currently accumulated time in milliseconds
Definition: timer.h:120
uint64 m_bytes
bytes processed
Definition: timer.h:234