|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r58986 - sandbox/chrono/boost/chrono
From: vicente.botet_at_[hidden]
Date: 2010-01-13 16:41:47
Author: viboes
Date: 2010-01-13 16:41:46 EST (Wed, 13 Jan 2010)
New Revision: 58986
URL: http://svn.boost.org/trac/boost/changeset/58986
Log:
Boost.Chrono: Version 0.3.0,
* Added digital_time fixed formatting
* Added time formatter following the formatting of the time script
* Adapt process_cpu_clock to the Clock requirements
Added:
sandbox/chrono/boost/chrono/time_formatter.hpp (contents, props changed)
Text files modified:
sandbox/chrono/boost/chrono/digital_time_formatter.hpp | 22 +++++++++++++----
sandbox/chrono/boost/chrono/process_cpu_clocks.hpp | 27 +++++++++++++++++++++
sandbox/chrono/boost/chrono/stopwatch_reporter.hpp | 50 ++-------------------------------------
3 files changed, 47 insertions(+), 52 deletions(-)
Modified: sandbox/chrono/boost/chrono/digital_time_formatter.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/digital_time_formatter.hpp (original)
+++ sandbox/chrono/boost/chrono/digital_time_formatter.hpp 2010-01-13 16:41:46 EST (Wed, 13 Jan 2010)
@@ -46,10 +46,6 @@
duration d = stopwatch_.elapsed( ec );
if ( d < duration(0) ) return;
- if ( places > 9 )
- places = 9; // sanity check
- else if ( places < 0 )
- places = 0;
boost::io::ios_flags_saver ifs( os );
os.setf( std::ios_base::fixed, std::ios_base::floatfield );
@@ -66,18 +62,34 @@
case 'd':
os << dt.days_.count();
break;
- case 'h':
+ case 'h':
+ {
+ boost::io::ios_flags_saver ifs( os );
+ os.width(2); os.fill('0');
os << dt.hours_.count();
break;
+ }
case 'm':
+ {
+ boost::io::ios_flags_saver ifs( os );
+ os.width(2); os.fill('0');
os << dt.minutes_.count();
break;
+ }
case 's':
+ {
+ boost::io::ios_flags_saver ifs( os );
+ os.width(2); os.fill('0');
os << dt.seconds_.count();
break;
+ }
case 'n':
+ {
+ boost::io::ios_flags_saver ifs( os );
+ os.width(9); os.fill('0');
os << dt.nanoseconds_.count();
break;
+ }
default:
assert(0 && "digital_time_formatter internal logic error");
}
Modified: sandbox/chrono/boost/chrono/process_cpu_clocks.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/process_cpu_clocks.hpp (original)
+++ sandbox/chrono/boost/chrono/process_cpu_clocks.hpp 2010-01-13 16:41:46 EST (Wed, 13 Jan 2010)
@@ -56,6 +56,10 @@
public:
struct times : arithmetic<times>, less_than_comparable<times>
{
+ times()
+ : real(0)
+ , user(0)
+ , system(0){}
times(
process_real_cpu_clock::rep r,
process_user_cpu_clock::rep u,
@@ -67,6 +71,12 @@
process_real_cpu_clock::rep real; // real (i.e wall clock) time
process_user_cpu_clock::rep user; // user cpu time
process_system_cpu_clock::rep system; // system cpu time
+
+ bool operator==(times const& rhs) {
+ return (real==rhs.real &&
+ user==rhs.user &&
+ system==rhs.system);
+ }
times operator+=(times const& rhs) {
real+=rhs.real;
@@ -111,6 +121,23 @@
static time_point now( system::error_code & ec = system::throws );
};
+ template <>
+ struct duration_values<process_cpu_clock::times>
+ {
+ typedef process_cpu_clock::times Rep;
+ public:
+ static Rep zero() {return Rep();}
+ static Rep max BOOST_PREVENT_MACRO_SUBSTITUTION () {
+ return Rep((std::numeric_limits<process_real_cpu_clock::rep>::max)(),
+ (std::numeric_limits<process_user_cpu_clock::rep>::max)(),
+ (std::numeric_limits<process_system_cpu_clock::rep>::max)());
+ }
+ static Rep min BOOST_PREVENT_MACRO_SUBSTITUTION () {
+ return Rep((std::numeric_limits<process_real_cpu_clock::rep>::min)(),
+ (std::numeric_limits<process_user_cpu_clock::rep>::min)(),
+ (std::numeric_limits<process_system_cpu_clock::rep>::min)());
+ }
+ };
} // namespace chrono
} // namespace boost
Modified: sandbox/chrono/boost/chrono/stopwatch_reporter.hpp
==============================================================================
--- sandbox/chrono/boost/chrono/stopwatch_reporter.hpp (original)
+++ sandbox/chrono/boost/chrono/stopwatch_reporter.hpp 2010-01-13 16:41:46 EST (Wed, 13 Jan 2010)
@@ -90,7 +90,7 @@
explicit stopwatch_reporter( const std::string & format,
system::error_code & ec = system::throws )
- : m_places(Formatter::m_default_places), m_os(Formatter::m_cout()), m_format(format), m_reported(false) { }
+ : m_places(Formatter::m_default_places), m_os(Formatter::m_cout()), m_format(format), m_reported(false) {}
explicit stopwatch_reporter( std::ostream & os, const std::string & format,
system::error_code & ec = system::throws )
@@ -120,8 +120,9 @@
~stopwatch_reporter() {// never throws
system::error_code ec;
//this->stop(ec);
- if ( !reported() )
+ if ( !reported() ) {
this->report( ec );
+ }
}
@@ -145,57 +146,12 @@
stopwatch_reporter& operator=(const stopwatch_reporter&); // = delete;
};
-
- namespace detail {
-
- template <class Stopwatch >
- void show_time( Stopwatch & stopwatch_, const char * format, int places, std::ostream & os, system::error_code & ec)
- // NOTE WELL: Will truncate least-significant digits to LDBL_DIG, which may
- // be as low as 10, although will be 15 for many common platforms.
- {
- typedef typename Stopwatch::duration duration;
- duration d = stopwatch_.elapsed( ec );
-
- if ( d < duration(0) ) return;
- if ( places > 9 )
- places = 9; // sanity check
- else if ( places < 0 )
- places = 0;
-
- boost::io::ios_flags_saver ifs( os );
- os.setf( std::ios_base::fixed, std::ios_base::floatfield );
- boost::io::ios_precision_saver ips( os );
- os.precision( places );
-
- for ( ; *format; ++format )
- {
- if ( *format != '%' || !*(format+1) || !std::strchr("d", *(format+1)) )
- os << *format;
-
- else
- {
- ++format;
- switch ( *format )
- {
- case 'd':
- os << boost::chrono::duration<double>(d).count();
- //os << d.count();
- break;
- default:
- assert(0 && "run_timer internal logic error");
- }
- }
- }
- }
- }
-
template <class Stopwatch, class Formatter>
void stopwatch_reporter<Stopwatch, Formatter>::report( system::error_code & ec ) {
m_reported = true;
if ( m_format.empty() ) m_format = Formatter::default_format;
//typename Stopwatch::duration d = this->elapsed( ec );
-
if ( &ec == &system::throws ) {
Formatter::show_time( *this, m_format.c_str(), m_places, m_os, ec);
} else {// non-throwing
Added: sandbox/chrono/boost/chrono/time_formatter.hpp
==============================================================================
--- (empty file)
+++ sandbox/chrono/boost/chrono/time_formatter.hpp 2010-01-13 16:41:46 EST (Wed, 13 Jan 2010)
@@ -0,0 +1,112 @@
+// boost/chrono/timer.hpp ------------------------------------------------------------//
+
+// Copyright 2009-2010 Vicente J. Botet Escriba
+
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// See http://www.boost.org/libs/system for documentation.
+
+#ifndef BOOST_CHRONO_TIME_FORMATTER_HPP
+#define BOOST_CHRONO_TIME_FORMATTER_HPP
+
+#include <boost/chrono/chrono.hpp>
+#include <boost/chrono/process_cpu_clocks.hpp>
+#include <boost/system/error_code.hpp>
+#include <boost/cstdint.hpp>
+#include <string>
+#include <iostream>
+#include <boost/io/ios_state.hpp>
+#include <cstring>
+#include <cassert>
+
+#include <boost/config/abi_prefix.hpp> // must be the last #include
+
+namespace boost { namespace chrono {
+
+//--------------------------------------------------------------------------------------//
+//--------------------------------------------------------------------------------------//
+
+
+ class time_formatter {
+ public:
+ static std::ostream & m_cout();
+ static const int m_default_places = 3;
+ static const char * default_format;
+ static int default_places() { return m_default_places; }
+
+ template <class Stopwatch >
+ static void show_time( Stopwatch & stopwatch_
+ , const char * format, int places, std::ostream & os
+ , system::error_code & ec)
+ // NOTE WELL: Will truncate least-significant digits to LDBL_DIG, which may
+ // be as low as 10, although will be 15 for many common platforms.
+ {
+ typedef typename Stopwatch::duration duration;
+ typedef typename duration::rep rep;
+ duration d = stopwatch_.elapsed( ec );
+ rep times=d.count();
+ if ( times.real < 0 ) return;
+ if ( places > 9 )
+ places = 9; // sanity check
+ else if ( places < 0 )
+ places = 0;
+
+ boost::io::ios_flags_saver ifs( os );
+ os.setf( std::ios_base::fixed, std::ios_base::floatfield );
+ boost::io::ios_precision_saver ips( os );
+ os.precision( places );
+
+ nanoseconds total = nanoseconds(times.system + times.user);
+
+ for ( ; *format; ++format )
+ {
+ if ( *format != '%' || !*(format+1) || !std::strchr("rcpus", *(format+1)) )
+ os << *format;
+ else
+ {
+ ++format;
+ switch ( *format )
+ {
+ case 'r':
+ os << boost::chrono::duration<double>(nanoseconds(times.real)).count();
+ break;
+ case 'u':
+ os << boost::chrono::duration<double>(nanoseconds(times.user)).count();
+ break;
+ case 's':
+ os << boost::chrono::duration<double>(nanoseconds(times.system)).count();
+ break;
+ case 'c':
+ os << boost::chrono::duration<double>(total).count();
+ break;
+ case 'p':
+ {
+ boost::io::ios_precision_saver ips( os );
+ os.precision( 1 );
+ if ( times.real && total.count() )
+ os << boost::chrono::duration<double>(total).count()
+ /boost::chrono::duration<double>(nanoseconds(times.real)).count() * 100.0;
+ else
+ os << 0.0;
+ }
+ break;
+ default:
+ assert(0 && "run_timer internal logic error");
+ }
+ }
+ }
+ }
+
+ };
+ const char * time_formatter::default_format = "\nreal %rs, cpu %cs (%p%), user %us, system %ss\n";
+
+
+ std::ostream & time_formatter::m_cout() { return std::cout; }
+
+ } // namespace chrono
+} // namespace boost
+
+#include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
+
+#endif // BOOST_CHRONO_TIME_FORMATTER_HPP
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk