Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74360 - in sandbox/stopwatches/libs/chrono/stopwatches: example test
From: vicente.botet_at_[hidden]
Date: 2011-09-12 12:57:25


Author: viboes
Date: 2011-09-12 12:57:24 EDT (Mon, 12 Sep 2011)
New Revision: 74360
URL: http://svn.boost.org/trac/boost/changeset/74360

Log:
Stopwatches: Update some examples of stopwatch reporters to use Boost.Format
Text files modified:
   sandbox/stopwatches/libs/chrono/stopwatches/example/scoped_stopwatch_example.cpp | 59 ++++++++++++++++---------
   sandbox/stopwatches/libs/chrono/stopwatches/example/stopwatch_accumulator_example.cpp | 89 +++++++++++++++++++++++++++++++++++----
   sandbox/stopwatches/libs/chrono/stopwatches/test/Jamfile.v2 | 4
   3 files changed, 117 insertions(+), 35 deletions(-)

Modified: sandbox/stopwatches/libs/chrono/stopwatches/example/scoped_stopwatch_example.cpp
==============================================================================
--- sandbox/stopwatches/libs/chrono/stopwatches/example/scoped_stopwatch_example.cpp (original)
+++ sandbox/stopwatches/libs/chrono/stopwatches/example/scoped_stopwatch_example.cpp 2011-09-12 12:57:24 EDT (Mon, 12 Sep 2011)
@@ -6,41 +6,56 @@
 
 #include <boost/chrono/stopwatches/stopwatches.hpp>
 #include <cmath>
+#include <string>
 #include "sleep_for.hpp"
 #include <boost/chrono/chrono_io.hpp>
-//#include <iostream>
+#include <boost/format.hpp>
 
 using namespace boost::chrono;
 
+template <typename Stopwatch>
+class stopwatch_reporter : public Stopwatch
+{
+ public:
+ stopwatch_reporter( )
+ : internal_fmt_("%1%"), fmt_(internal_fmt_) {}
+ stopwatch_reporter(const char* str )
+ : internal_fmt_(str), fmt_(internal_fmt_) {}
+ stopwatch_reporter(std::string const& str )
+ : internal_fmt_(str), fmt_(internal_fmt_) {}
+ stopwatch_reporter(boost::format & fmt )
+ : fmt_(fmt) {}
+ ~stopwatch_reporter() {
+ std::cout << fmt_ % this->elapsed() << std::endl;
+ }
+ private:
+ boost::format internal_fmt_;
+ boost::format& fmt_;
+};
+
 long double res;
 void f1(long j)
 {
- //stopwatch_reporter<stopwatch<> > _(BOOST_STOPWATCHES_STOPWATCH_FUNCTION_FORMAT);
- //stopwatch<> sw;
- for (long i =0; i< j; i+=1)
- res+=std::sqrt( res+123.456L+i ); // burn some time
- if (j!=0) f1(j-1);
- //stopwatch_reporter<stopwatch<> >::scoped_suspend s(_);
- //std::cout << "f1("<< j <<") Elapsed time: " << sw.elapsed() << std::endl;
-
+ //stopwatch_reporter<stopwatch<> > _(BOOST_STOPWATCHES_STOPWATCH_FUNCTION_FORMAT);
+ boost::format fmt("f1(%1%): Elapsed time: %2%");
+ fmt % j;
+ ::stopwatch_reporter<stopwatch<> > sw(fmt);
+ for (long i =0; i< j; i+=1)
+ res+=std::sqrt( res+123.456L+i ); // burn some time
+ stopwatch_suspender< ::stopwatch_reporter<stopwatch<> > > _(sw);
+ boost::this_thread::sleep_for(boost::chrono::milliseconds(10));
 }
+
 int main()
 {
   //stopwatch_reporter<stopwatch<> > _(BOOST_STOPWATCHES_STOPWATCH_FUNCTION_FORMAT);
- stopwatch<> sw;
+ ::stopwatch_reporter<stopwatch<> > _("main(): Elapsed time: %1%");
 
- res=0;
- for (long i =1; i< 4; ++i) {
- stopwatch<> sw;
- f1(i*1000);
- {
- stopwatch_suspender<stopwatch<> > _(sw);
- boost::this_thread::sleep_for(boost::chrono::milliseconds(10));
- }
- std::cout << "f1("<< i*1000 <<") Elapsed time: " << sw.elapsed() << std::endl;
- }
+ res=0;
+ for (long i =1; i< 4; ++i) {
+ f1(i*1000000);
+ }
 
- std::cout<< res << std::endl;
- std::cout << "main() Elapsed time: " << sw.elapsed() << std::endl;
+ std::cout<< res << std::endl;
   return 0;
 }

Modified: sandbox/stopwatches/libs/chrono/stopwatches/example/stopwatch_accumulator_example.cpp
==============================================================================
--- sandbox/stopwatches/libs/chrono/stopwatches/example/stopwatch_accumulator_example.cpp (original)
+++ sandbox/stopwatches/libs/chrono/stopwatches/example/stopwatch_accumulator_example.cpp 2011-09-12 12:57:24 EDT (Mon, 12 Sep 2011)
@@ -8,12 +8,71 @@
 #include <boost/chrono/stopwatches/stopwatches.hpp>
 //#include <boost/chrono/process_cpu_clocks.hpp>
 #include <cmath>
+#include <iostream>
+#include <iomanip>
+#include <boost/format.hpp>
+#include <boost/format/group.hpp>
+#include <boost/chrono/chrono_io.hpp>
 
 
 using namespace boost::chrono;
 using namespace boost;
 
+std::ostream&
+my_duration_short(std::ostream& os)
+{
+ return boost::chrono::duration_short(os);
+}
 
+template <typename Stopwatch, typename Ratio=boost::ratio<1> >
+class stopwatch_reporter : public Stopwatch
+{
+ public:
+ stopwatch_reporter( )
+ : internal_fmt_("times=%1%, sum=%2%, min=%3%, max=%4%, mean=%5%, frequency=%6% Hz, lifetime=%7%, time=%8% %%"),
+ fmt_(internal_fmt_),
+ os_(std::cout)
+ {}
+ stopwatch_reporter(const char* str )
+ : internal_fmt_(str),
+ fmt_(internal_fmt_),
+ os_(std::cout)
+ {}
+ stopwatch_reporter(std::string const& str )
+ : internal_fmt_(str),
+ fmt_(internal_fmt_),
+ os_(std::cout)
+ {}
+ stopwatch_reporter(boost::format & fmt )
+ : fmt_(fmt),
+ os_(std::cout)
+ {}
+ ~stopwatch_reporter() {
+ typename Stopwatch::accumulator_set& acc = this->get_storage();
+ typedef typename Stopwatch::duration duration_t;
+ int precision_=3;
+
+ os_ << fmt_
+ % boost::accumulators::count(acc)
+ % io::group(std::fixed, std::setprecision(precision_),my_duration_short, boost::chrono::duration<double,Ratio>(duration_t(boost::accumulators::sum(acc))))
+ % io::group(std::fixed, std::setprecision(precision_),my_duration_short, boost::chrono::duration<double,Ratio>(duration_t((boost::accumulators::min)(acc))))
+ % io::group(std::fixed, std::setprecision(precision_),my_duration_short, boost::chrono::duration<double,Ratio>(duration_t((boost::accumulators::max)(acc))))
+ % io::group(std::fixed, std::setprecision(precision_),my_duration_short,((boost::accumulators::count(acc)>0)
+ ? boost::chrono::duration<double,Ratio>(duration_t(boost::accumulators::sum(acc)/boost::accumulators::count(acc)))
+ : boost::chrono::duration<double,Ratio>(duration_t(0))))
+ % io::group(std::fixed, std::setprecision(precision_),
+ ((boost::accumulators::count(acc)>0)
+ ? boost::accumulators::count(acc)/boost::chrono::duration<double>(this->lifetime())
+ : 0))
+ % io::group(std::fixed, std::setprecision(precision_),my_duration_short, boost::chrono::duration<double,Ratio>(this->lifetime()))
+ % io::group(std::fixed, std::setprecision(precision_),boost::chrono::duration<double>(duration_t(accumulators::sum(acc))).count()*100/boost::chrono::duration<double>(this->lifetime()).count())
+ << std::endl;
+ }
+ private:
+ boost::format internal_fmt_;
+ boost::format& fmt_;
+ std::ostream& os_;
+};
 
 //typedef stopwatch_accumulator<process_real_cpu_clock> swa;
 typedef stopwatch_accumulator<high_resolution_clock> swa;
@@ -21,10 +80,16 @@
 {
   //static stopwatch_reporter<stopwatch_accumulator<high_resolution_clock> > acc(BOOST_STOPWATCHES_ACCUMULATOR_FUNCTION_FORMAT);
   //stopwatch_reporter<stopwatch_accumulator<high_resolution_clock> >::scoped_run r_(acc);
+ static ::stopwatch_reporter<stopwatch_accumulator<high_resolution_clock>, boost::micro> acc;
+ stopwatch_runner<
+ ::stopwatch_reporter<
+ stopwatch_accumulator<high_resolution_clock>, boost::micro
+ >
+ > r_(acc);
   //static stopwatch_reporter<stopwatch_accumulator<process_real_cpu_clock> > acc2(BOOST_STOPWATCHES_ACCUMULATOR_FUNCTION_FORMAT);
   //stopwatch_reporter<stopwatch_accumulator<process_real_cpu_clock> >::scoped_run r2_(acc2);
 
- stopwatch_runner<swa > _(sw);
+ //stopwatch_runner<swa > _(sw);
 
   for ( long i = 0; i < j; ++i )
     std::sqrt( 123.456L ); // burn some time
@@ -35,18 +100,20 @@
 {
   swa sw;
 
+ std::cout << duration_short << nanoseconds(10) << std::endl;
+
   f1(10000000,sw);
   f1(20000000,sw);
   f1(30000000,sw);
 
- swa::accumulator_set& acc = sw.get_storage();
- std::cout << "f1() Lifetime: " << sw.lifetime() << std::endl;
- std::cout << "f1() Elapsed: " << sw.elapsed() << std::endl;
- std::cout << "f1() Count: " << accumulators::count(acc) << std::endl;
- std::cout << "f1() Sum: " << accumulators::sum(acc) << std::endl;
- std::cout << "f1() Sum/Count: " << accumulators::sum(acc)/accumulators::count(acc) << std::endl;
- std::cout << "f1() Mean: " << accumulators::mean(acc) << std::endl;
- std::cout << "f1() Min: " << (accumulators::min)(acc) << std::endl;
- std::cout << "f1() Max: " << (accumulators::max)(acc) << std::endl;
- return 0;
+// swa::accumulator_set& acc = sw.get_storage();
+// std::cout << "f1() Lifetime: " << sw.lifetime() << std::endl;
+// std::cout << "f1() Elapsed: " << sw.elapsed() << std::endl;
+// std::cout << "f1() Count: " << accumulators::count(acc) << std::endl;
+// std::cout << "f1() Sum: " << accumulators::sum(acc) << std::endl;
+// std::cout << "f1() Sum/Count: " << accumulators::sum(acc)/accumulators::count(acc) << std::endl;
+// std::cout << "f1() Mean: " << accumulators::mean(acc) << std::endl;
+// std::cout << "f1() Min: " << (accumulators::min)(acc) << std::endl;
+// std::cout << "f1() Max: " << (accumulators::max)(acc) << std::endl;
+ return 1;
 }

Modified: sandbox/stopwatches/libs/chrono/stopwatches/test/Jamfile.v2
==============================================================================
--- sandbox/stopwatches/libs/chrono/stopwatches/test/Jamfile.v2 (original)
+++ sandbox/stopwatches/libs/chrono/stopwatches/test/Jamfile.v2 2011-09-12 12:57:24 EDT (Mon, 12 Sep 2011)
@@ -25,9 +25,9 @@
         <library>/boost/system//boost_system
         #<library>/boost/thread//boost_thread/<link>shared
         # uncomment the line above if you build outside the Boost release
- <include>$(BOOST)
- # uncomment the line above if you build outside the Boost release
         <include>../../../..
+ # uncomment the line above if you build outside the Boost release
+ <include>$(BOOST)
         <toolset>msvc:<asynch-exceptions>on
         <define>BOOST_ENABLE_WARNINGS
         <define>BOOST_CHRONO_EXTENSIONS


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