STXXL  1.4.1
 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 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 class timer
65 {
66  bool running;
67  double accumulated;
68  double last_clock;
69 
70  //! return current timestamp
71  static inline double timestamp()
72  {
73  return stxxl::timestamp();
74  }
75 
76 public:
77  inline timer(bool start_immediately = false)
78  : running(false), accumulated(0.), last_clock(0)
79  {
80  if (start_immediately) start();
81  }
82 
83  //! start timer
84  inline void start()
85  {
86  running = true;
87  last_clock = timestamp();
88  }
89 
90  //! stop timer
91  inline void stop()
92  {
93  running = false;
94  accumulated += timestamp() - last_clock;
95  }
96 
97  //! return accumulated time
98  inline void reset()
99  {
100  accumulated = 0.;
101  last_clock = timestamp();
102  }
103 
104  //! return currently accumulated time in milliseconds
105  inline double mseconds()
106  {
107  if (running)
108  return (accumulated + timestamp() - last_clock) * 1000.;
109 
110  return (accumulated * 1000.);
111  }
112 
113  //! return currently accumulated time in microseconds
114  inline double useconds()
115  {
116  if (running)
117  return (accumulated + timestamp() - last_clock) * 1000000.;
118 
119  return (accumulated * 1000000.);
120  }
121 
122  //! return currently accumulated time in seconds (as double)
123  inline double seconds()
124  {
125  if (running)
126  return (accumulated + timestamp() - last_clock);
127 
128  return (accumulated);
129  }
130 };
131 
132 /*!
133  * Simple scoped timer, which takes a text message and prints the duration
134  * until the scope is destroyed.
135  */
137 {
138 protected:
139  //! message
140  std::string m_message;
141 
142  //! bytes processed
144 
145  //! timer
146  class timer m_timer;
147 
148 public:
149  //! save message and start timer
150  scoped_print_timer(const std::string& message, const uint64 bytes = 0)
151  : m_message(message),
152  m_bytes(bytes),
153  m_timer(true)
154  {
155  STXXL_MSG("Starting " << message);
156  }
157 
158  //! on destruction: tell the time
160  {
161  if (m_bytes == 0) {
162  STXXL_MSG("Finished "
163  << m_message
164  << " after " << m_timer.seconds() << " seconds");
165  }
166  else {
167  double bps = (double)m_bytes / m_timer.seconds();
168 
169  STXXL_MSG("Finished "
170  << m_message
171  << " after " << m_timer.seconds() << " seconds. "
172  << "Processed " << format_IEC_size(m_bytes) << "B"
173  << " @ " << format_IEC_size((uint64)bps) << "B/s");
174  }
175  }
176 };
177 
178 //! \}
179 
181 
182 #endif // !STXXL_COMMON_TIMER_HEADER
183 // vim: et:ts=4:sw=4
~scoped_print_timer()
on destruction: tell the time
Definition: timer.h:159
bool running
Definition: timer.h:66
double mseconds()
return currently accumulated time in milliseconds
Definition: timer.h:105
std::string m_message
message
Definition: timer.h:140
unsigned long long int uint64
Definition: types.h:39
void start()
start timer
Definition: timer.h:84
Simple scoped timer, which takes a text message and prints the duration until the scope is destroyed...
Definition: timer.h:136
void stop()
stop timer
Definition: timer.h:91
void reset()
return accumulated time
Definition: timer.h:98
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
timer(bool start_immediately=false)
Definition: timer.h:77
double seconds()
return currently accumulated time in seconds (as double)
Definition: timer.h:123
double useconds()
return currently accumulated time in microseconds
Definition: timer.h:114
static double timestamp()
return current timestamp
Definition: timer.h:71
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
double accumulated
Definition: timer.h:67
double last_clock
Definition: timer.h:68
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:150
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
uint64 m_bytes
bytes processed
Definition: timer.h:143