1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
5 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
6 Software License, Version 1.0. (See accompanying file
7 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
10 #if !defined(STOP_WATCH_HPP_HK040911_INCLUDED)
11 #define STOP_WATCH_HPP_HK040911_INCLUDED
13 #include <boost/config.hpp>
14 #include <boost/timer.hpp>
16 ///////////////////////////////////////////////////////////////////////////////
18 class stop_watch : public boost::timer {
20 typedef boost::timer base_t;
23 stop_watch() : is_suspended_since(0), suspended_overall(0) {}
27 if (0 == is_suspended_since) {
28 // if not already suspended
29 is_suspended_since = this->base_t::elapsed();
34 if (0 != is_suspended_since) {
35 // if really suspended
36 suspended_overall += this->base_t::elapsed() - is_suspended_since;
37 is_suspended_since = 0;
40 double elapsed() const
42 if (0 == is_suspended_since) {
44 return this->base_t::elapsed() - suspended_overall;
47 // currently suspended
48 BOOST_ASSERT(is_suspended_since >= suspended_overall);
49 return is_suspended_since - suspended_overall;
52 std::string format_elapsed_time() const
54 double current = elapsed();
55 char time_buffer[sizeof("1234:56:78.90 abcd.")+1];
58 if (current >= 3600) {
60 sprintf (time_buffer, "%d:%02d:%02d.%03d hrs.",
61 (int)(current) / 3600, ((int)(current) % 3600) / 60,
62 ((int)(current) % 3600) % 60,
63 (int)(current * 1000) % 1000);
65 else if (current >= 60) {
67 sprintf (time_buffer, "%d:%02d.%03d min.",
68 (int)(current) / 60, (int)(current) % 60,
69 (int)(current * 1000) % 1000);
73 sprintf(time_buffer, "%d.%03d sec.", (int)current,
74 (int)(current * 1000) % 1000);
80 double is_suspended_since;
81 double suspended_overall;
84 #endif // !defined(STOP_WATCH_HPP_HK040911_INCLUDED)