STXXL  1.4.0
 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 
37 
39 
40 //! Returns number of seconds since the epoch, high resolution.
41 inline double
43 {
44 #if STXXL_BOOST_TIMESTAMP
45  boost::posix_time::ptime MyTime = boost::posix_time::microsec_clock::local_time();
46  boost::posix_time::time_duration Duration =
47  MyTime - boost::posix_time::time_from_string("1970-01-01 00:00:00.000");
48  double sec = double(Duration.hours()) * 3600. +
49  double(Duration.minutes()) * 60. +
50  double(Duration.seconds()) +
51  double(Duration.fractional_seconds()) / (pow(10., Duration.num_fractional_digits()));
52  return sec;
53 #elif STXXL_WINDOWS
54  return GetTickCount() / 1000.0;
55 #else
56  struct timeval tp;
57  gettimeofday(&tp, NULL);
58  return double(tp.tv_sec) + tp.tv_usec / 1000000.;
59 #endif
60 }
61 
62 class timer
63 {
64  bool running;
65  double accumulated;
66  double last_clock;
67 
68  //! return current timestamp
69  static inline double timestamp()
70  {
71  return stxxl::timestamp();
72  }
73 
74 public:
75  inline timer(bool start_immediately = false)
76  : running(false), accumulated(0.), last_clock(0)
77  {
78  if (start_immediately) start();
79  }
80 
81  //! start timer
82  inline void start()
83  {
84  running = true;
85  last_clock = timestamp();
86  }
87 
88  //! stop timer
89  inline void stop()
90  {
91  running = false;
92  accumulated += timestamp() - last_clock;
93  }
94 
95  //! return accumulated time
96  inline void reset()
97  {
98  accumulated = 0.;
99  last_clock = timestamp();
100  }
101 
102  //! return currently accumulated time in milliseconds
103  inline double mseconds()
104  {
105  if (running)
106  return (accumulated + timestamp() - last_clock) * 1000.;
107 
108  return (accumulated * 1000.);
109  }
110 
111  //! return currently accumulated time in microseconds
112  inline double useconds()
113  {
114  if (running)
115  return (accumulated + timestamp() - last_clock) * 1000000.;
116 
117  return (accumulated * 1000000.);
118  }
119 
120  //! return currently accumulated time in seconds (as double)
121  inline double seconds()
122  {
123  if (running)
124  return (accumulated + timestamp() - last_clock);
125 
126  return (accumulated);
127  }
128 };
129 
130 /*!
131  * Simple scoped timer, which takes a text message and prints the duration
132  * until the scope is destroyed.
133  */
135 {
136 protected:
137  //! message
138  std::string m_message;
139 
140  //! bytes processed
142 
143  //! timer
144  class timer m_timer;
145 
146 public:
147  //! save message and start timer
148  scoped_print_timer(const std::string& message, const uint64 bytes = 0)
149  : m_message(message),
150  m_bytes(bytes),
151  m_timer(true)
152  {
153  STXXL_MSG("Starting " << message);
154  }
155 
156  //! on destruction: tell the time
158  {
159  if (m_bytes == 0)
160  STXXL_MSG("Finished " << m_message
161  << " after " << m_timer.seconds() << " seconds");
162  else
163  STXXL_MSG("Finished " << m_message
164  << " after " << m_timer.seconds() << " seconds. "
165  << "Processed " << format_IEC_size(m_bytes) << "B"
166  << " @ " << format_IEC_size(uint64(m_bytes / m_timer.seconds())) << "B/s");
167  }
168 };
169 
171 
172 #endif // !STXXL_COMMON_TIMER_HEADER
173 // vim: et:ts=4:sw=4
~scoped_print_timer()
on destruction: tell the time
Definition: timer.h:157
bool running
Definition: timer.h:64
static const int bytes
number of bytes in uint_pair
Definition: uint_types.h:99
double mseconds()
return currently accumulated time in milliseconds
Definition: timer.h:103
std::string m_message
message
Definition: timer.h:138
unsigned long long int uint64
Definition: types.h:41
void start()
start timer
Definition: timer.h:82
Simple scoped timer, which takes a text message and prints the duration until the scope is destroyed...
Definition: timer.h:134
void stop()
stop timer
Definition: timer.h:89
double timestamp()
Returns number of seconds since the epoch, high resolution.
Definition: timer.h:42
void reset()
return accumulated time
Definition: timer.h:96
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
timer(bool start_immediately=false)
Definition: timer.h:75
double seconds()
return currently accumulated time in seconds (as double)
Definition: timer.h:121
double useconds()
return currently accumulated time in microseconds
Definition: timer.h:112
static double timestamp()
return current timestamp
Definition: timer.h:69
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:65
double last_clock
Definition: timer.h:66
#define STXXL_MSG(x)
Definition: verbose.h:72
scoped_print_timer(const std::string &message, const uint64 bytes=0)
save message and start timer
Definition: timer.h:148
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
uint64 m_bytes
bytes processed
Definition: timer.h:141