1 /*============================================================================= 2 Boost.Wave: A Standard compliant C++ preprocessor library 3 http://www.boost.org/ 4 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 =============================================================================*/ 9 10 #if !defined(BOOST_STOP_WATCH_HPP_HK040911_INCLUDED) 11 #define BOOST_STOP_WATCH_HPP_HK040911_INCLUDED 12 13 #include <boost/config.hpp> 14 #include <boost/timer/timer.hpp> 15 16 /////////////////////////////////////////////////////////////////////////////// 17 // 18 class stop_watch : public boost::timer::cpu_timer { 19 20 public: 21 format_elapsed_time() const22 std::string format_elapsed_time() const 23 { 24 boost::timer::cpu_times times = elapsed(); 25 double current = static_cast<double>(times.user + times.system) / 1.e9; 26 27 char time_buffer[sizeof("1234:56:78.90 abcd.")+1]; 28 29 using namespace std; 30 if (current >= 3600) { 31 // show hours 32 sprintf (time_buffer, "%d:%02d:%02d.%03d hrs.", 33 (int)(current) / 3600, ((int)(current) % 3600) / 60, 34 ((int)(current) % 3600) % 60, 35 (int)(current * 1000) % 1000); 36 } 37 else if (current >= 60) { 38 // show minutes 39 sprintf (time_buffer, "%d:%02d.%03d min.", 40 (int)(current) / 60, (int)(current) % 60, 41 (int)(current * 1000) % 1000); 42 } 43 else { 44 // show seconds 45 sprintf(time_buffer, "%d.%03d sec.", (int)current, 46 (int)(current * 1000) % 1000); 47 } 48 return time_buffer; 49 } 50 51 }; 52 53 #endif // !defined(BOOST_STOP_WATCH_HPP_HK040911_INCLUDED) 54