vSMC
vSMC: Scalable Monte Carlo
stop_watch.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/utility/stop_watch.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2015, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_UTILITY_STOP_WATCH_HPP
33 #define VSMC_UTILITY_STOP_WATCH_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 
39 #ifndef VSMC_STOP_WATCH_CLOCK_TYPE
40 #define VSMC_STOP_WATCH_CLOCK_TYPE std::chrono::high_resolution_clock
41 #endif
42 
43 namespace vsmc
44 {
45 
49 template <typename WatchType>
51 {
52  public:
53  using watch_type = WatchType;
54 
55  StopWatchGuard(watch_type &watch, bool start = true)
56  : start_(start), watch_(watch)
57  {
58  if (start_)
59  watch_.start();
60  }
61 
63  {
64  if (start_)
65  watch_.stop();
66  }
67 
68  private:
69  const bool start_;
70  watch_type &watch_;
71 }; // class StopWatchGuard
72 
75 template <typename ClockType>
77 {
78  public:
79  using clock_type = ClockType;
80 
81  StopWatchClockAdapter() : elapsed_(0), running_(false) { reset(); }
82 
88  bool running() const { return running_; }
89 
97  bool start()
98  {
99  if (running_)
100  return false;
101 
102  running_ = true;
103  start_time_ = clock_type::now();
104 
105  return true;
106  }
107 
113  bool stop()
114  {
115  if (!running_)
116  return false;
117 
118  typename clock_type::time_point stop_time = clock_type::now();
119  elapsed_ += stop_time - start_time_;
120  running_ = false;
121 
122  return true;
123  }
124 
126  void reset()
127  {
128  start();
129  elapsed_ = typename clock_type::duration(0);
130  running_ = false;
131  }
132 
134  double nanoseconds() const
135  {
136  return std::chrono::duration_cast<
137  std::chrono::duration<double, std::nano>>(elapsed_)
138  .count();
139  }
140 
142  double microseconds() const
143  {
144  return std::chrono::duration_cast<
145  std::chrono::duration<double, std::micro>>(elapsed_)
146  .count();
147  }
148 
150  double milliseconds() const
151  {
152  return std::chrono::duration_cast<
153  std::chrono::duration<double, std::milli>>(elapsed_)
154  .count();
155  }
156 
158  double seconds() const
159  {
160  return std::chrono::duration_cast<
161  std::chrono::duration<double, std::ratio<1>>>(elapsed_)
162  .count();
163  }
164 
166  double minutes() const
167  {
168  return std::chrono::duration_cast<
169  std::chrono::duration<double, std::ratio<60>>>(elapsed_)
170  .count();
171  }
172 
174  double hours() const
175  {
176  return std::chrono::duration_cast<
177  std::chrono::duration<double, std::ratio<3600>>>(elapsed_)
178  .count();
179  }
180 
181  private:
182  typename clock_type::duration elapsed_;
183  typename clock_type::time_point start_time_;
184  bool running_;
185 }; // class StopWatchClockAdapter
186 
190 
191 } // namespace vsmc
192 
193 #endif // VSMC_UTILITY_STOP_WATCH_HPP
Definition: monitor.hpp:49
double nanoseconds() const
Return the accumulated elapsed time in nanoseconds.
Definition: stop_watch.hpp:134
double hours() const
Return the accumulated elapsed time in hours.
Definition: stop_watch.hpp:174
void reset()
Stop and reset the elapsed time to zero.
Definition: stop_watch.hpp:126
StopWatchGuard(watch_type &watch, bool start=true)
Definition: stop_watch.hpp:55
double minutes() const
Return the accumulated elapsed time in minutes.
Definition: stop_watch.hpp:166
StopWatch as an adapter of C++11 clock.
Definition: stop_watch.hpp:76
bool stop()
Stop the watch, no effect if already stopped.
Definition: stop_watch.hpp:113
double microseconds() const
Return the accumulated elapsed time in microseconds.
Definition: stop_watch.hpp:142
double milliseconds() const
Return the accumulated elapsed time in milliseconds.
Definition: stop_watch.hpp:150
bool start()
Start the watch, no effect if already started.
Definition: stop_watch.hpp:97
Start and stop a StopWatch in scope (similiar to a mutex lock guard)
Definition: stop_watch.hpp:50
bool running() const
If the watch is running.
Definition: stop_watch.hpp:88
double seconds() const
Return the accumulated elapsed time in seconds.
Definition: stop_watch.hpp:158