From 7c8b8d52e0caebc3e3cfa933f8f7d8cd5f691578 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 7 Jul 2000 16:04:40 +0000 Subject: [PATCH 1/2] This commit was generated by cvs2svn to compensate for changes in r4, which included commits to RCS files with non-trunk default branches. [SVN r7621] --- .gitattributes | 96 ++++++++++++++++++++++++++++++++++++++ include/boost/progress.hpp | 81 ++++++++++++++++++++++++++++++++ include/boost/timer.hpp | 57 ++++++++++++++++++++++ prg_display.cpp | 43 +++++++++++++++++ prg_timer.cpp | 40 ++++++++++++++++ timer.cpp | 53 +++++++++++++++++++++ timer_test.cpp | 83 ++++++++++++++++++++++++++++++++ 7 files changed, 453 insertions(+) create mode 100644 .gitattributes create mode 100644 include/boost/progress.hpp create mode 100644 include/boost/timer.hpp create mode 100644 prg_display.cpp create mode 100644 prg_timer.cpp create mode 100644 timer.cpp create mode 100644 timer_test.cpp diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..3e84d7c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,96 @@ +* text=auto !eol svneol=native#text/plain +*.gitattributes text svneol=native#text/plain + +# Scriptish formats +*.bat text svneol=native#text/plain +*.bsh text svneol=native#text/x-beanshell +*.cgi text svneol=native#text/plain +*.cmd text svneol=native#text/plain +*.js text svneol=native#text/javascript +*.php text svneol=native#text/x-php +*.pl text svneol=native#text/x-perl +*.pm text svneol=native#text/x-perl +*.py text svneol=native#text/x-python +*.sh eol=lf svneol=LF#text/x-sh +configure eol=lf svneol=LF#text/x-sh + +# Image formats +*.bmp binary svneol=unset#image/bmp +*.gif binary svneol=unset#image/gif +*.ico binary svneol=unset#image/ico +*.jpeg binary svneol=unset#image/jpeg +*.jpg binary svneol=unset#image/jpeg +*.png binary svneol=unset#image/png +*.tif binary svneol=unset#image/tiff +*.tiff binary svneol=unset#image/tiff +*.svg text svneol=native#image/svg%2Bxml + +# Data formats +*.pdf binary svneol=unset#application/pdf +*.avi binary svneol=unset#video/avi +*.doc binary svneol=unset#application/msword +*.dsp text svneol=crlf#text/plain +*.dsw text svneol=crlf#text/plain +*.eps binary svneol=unset#application/postscript +*.gz binary svneol=unset#application/gzip +*.mov binary svneol=unset#video/quicktime +*.mp3 binary svneol=unset#audio/mpeg +*.ppt binary svneol=unset#application/vnd.ms-powerpoint +*.ps binary svneol=unset#application/postscript +*.psd binary svneol=unset#application/photoshop +*.rdf binary svneol=unset#text/rdf +*.rss text svneol=unset#text/xml +*.rtf binary svneol=unset#text/rtf +*.sln text svneol=native#text/plain +*.swf binary svneol=unset#application/x-shockwave-flash +*.tgz binary svneol=unset#application/gzip +*.vcproj text svneol=native#text/xml +*.vcxproj text svneol=native#text/xml +*.vsprops text svneol=native#text/xml +*.wav binary svneol=unset#audio/wav +*.xls binary svneol=unset#application/vnd.ms-excel +*.zip binary svneol=unset#application/zip + +# Text formats +.htaccess text svneol=native#text/plain +*.bbk text svneol=native#text/xml +*.cmake text svneol=native#text/plain +*.css text svneol=native#text/css +*.dtd text svneol=native#text/xml +*.htm text svneol=native#text/html +*.html text svneol=native#text/html +*.ini text svneol=native#text/plain +*.log text svneol=native#text/plain +*.mak text svneol=native#text/plain +*.qbk text svneol=native#text/plain +*.rst text svneol=native#text/plain +*.sql text svneol=native#text/x-sql +*.txt text svneol=native#text/plain +*.xhtml text svneol=native#text/xhtml%2Bxml +*.xml text svneol=native#text/xml +*.xsd text svneol=native#text/xml +*.xsl text svneol=native#text/xml +*.xslt text svneol=native#text/xml +*.xul text svneol=native#text/xul +*.yml text svneol=native#text/plain +boost-no-inspect text svneol=native#text/plain +CHANGES text svneol=native#text/plain +COPYING text svneol=native#text/plain +INSTALL text svneol=native#text/plain +Jamfile text svneol=native#text/plain +Jamroot text svneol=native#text/plain +Jamfile.v2 text svneol=native#text/plain +Jamrules text svneol=native#text/plain +Makefile* text svneol=native#text/plain +README text svneol=native#text/plain +TODO text svneol=native#text/plain + +# Code formats +*.c text svneol=native#text/plain +*.cpp text svneol=native#text/plain +*.h text svneol=native#text/plain +*.hpp text svneol=native#text/plain +*.ipp text svneol=native#text/plain +*.tpp text svneol=native#text/plain +*.jam text svneol=native#text/plain +*.java text svneol=native#text/plain diff --git a/include/boost/progress.hpp b/include/boost/progress.hpp new file mode 100644 index 0000000..0a91159 --- /dev/null +++ b/include/boost/progress.hpp @@ -0,0 +1,81 @@ +// boost progress.hpp header file ------------------------------------------// + +// (C) Copyright Beman Dawes 1994-99. Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// See http://www.boost.org for most recent version including documentation. + +// Revision History +// 22 Jul 99 Name changed to .hpp +// 16 Jul 99 Second beta +// 6 Jul 99 Initial boost version + +#ifndef BOOST_PROGRESS_HPP +#define BOOST_PROGRESS_HPP + +#include +#include +#include + +namespace boost { + +// progress_timer ----------------------------------------------------------// + +// A progress_timer behaves like a timer except that the destructor displays +// an elapsed time message at an appropriate place in an appropriate form. + +class progress_timer : public timer, noncopyable { + +public: + progress_timer() : _os(0) {} + progress_timer( std::ostream& os ) // os is hint; implementation may ignore + : _os(&os) {} + ~progress_timer(); + +private: + std::ostream* _os; // may be 0, also member may not be present in all imps + }; // progress_display + + +// progress_display --------------------------------------------------------// + +// progress_display displays an appropriate indication of +// progress at an appropriate place in an appropriate form. + +class progress_display : noncopyable { + +public: + progress_display( unsigned long expected_count ) + : _os(0) { restart(expected_count); } + progress_display( unsigned long expected_count, + std::ostream& os ) // os is hint; implementation may ignore + : _os(&os) { restart(expected_count); } + + void restart( unsigned long expected_count ); + // Effects: display appropriate scale + // Postconditions: count()==0, expected_count()==expected_count + + unsigned long operator+=( unsigned long increment ) { + if ( (_count += increment) >= _next_tic_count ) { display_tic(); } + return _count; + } // operator+= + // Effects: Display appropriate progress tic if needed. + // Postconditions: count()== original count() + increment + // Returns: count(). + + unsigned long operator++() { return operator+=( 1 ); } + unsigned long count() const { return _count; } + unsigned long expected_count() const { return _expected_count; } + +private: + std::ostream* _os; // may be 0, also member may not be present in all imps + unsigned long _count, _expected_count, _next_tic_count; + unsigned int _tic; + void display_tic(); + }; // progress_display + +} // namespace boost + +#endif // BOOST_PROGRESS_HPP diff --git a/include/boost/timer.hpp b/include/boost/timer.hpp new file mode 100644 index 0000000..6ccb436 --- /dev/null +++ b/include/boost/timer.hpp @@ -0,0 +1,57 @@ +// boost timer.hpp header file ---------------------------------------------// + +// (C) Copyright Beman Dawes 1994-99. Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// See http://www.boost.org for most recent version including documentation. + +// Revision History +// 25 Sep 99 elapsed_max() and elapsed_min() added (John Maddock) +// 16 Jul 99 Second beta +// 6 Jul 99 Initial boost version + +#ifndef BOOST_TIMER_HPP +#define BOOST_TIMER_HPP + +#include + +namespace boost { + +// timer -------------------------------------------------------------------// + +// A timer object measures elapsed time. + +// It is recommended that implementations measure wall clock rather than CPU +// time since the intended use is performance measurement on systems where +// total elapsed time is more important than just process or CPU time. + +// Warnings: The maximum measurable elapsed time may well be only 596.5+ hours +// due to implementation limitations. The accuracy of timings depends on the +// accuracy of timing information provided by the underlying platform, and +// this varies a great deal from platform to platform. + +class timer { + +public: + timer(); // postcondition: elapsed()==0 + timer( const timer& src ); // post: elapsed()==src.elapsed() + ~timer(); + timer& operator=( const timer& src ); // post: elapsed()==src.elapsed() + void restart(); // post: elapsed()==0 + double elapsed() const; // return elapsed time in seconds + + double elapsed_max() const; // return estimated maximum value for elapsed() + // Portability warning: elapsed_max() may return too high a value on systems + // where std::clock_t overflows or resets at surprising values. + double elapsed_min() const; // return minimum value for elapsed() + +private: + class _timer; + scoped_ptr<_timer> _imp; // hide implementation details + }; // timer + +} // namespace boost + +#endif // BOOST_TIMER_HPP diff --git a/prg_display.cpp b/prg_display.cpp new file mode 100644 index 0000000..01755e7 --- /dev/null +++ b/prg_display.cpp @@ -0,0 +1,43 @@ +// progress_display implementation -----------------------------------------// + +// (C) Copyright Beman Dawes 1996-99. Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// Revision History +// 16 Jul 99 Second beta +// 6 Jul 99 Initial boost version + +#include +#include +#include + +namespace boost { + +void progress_display::restart( unsigned long expected_count ) { + + _count = _next_tic_count = _tic = 0; + _expected_count = expected_count; + + if ( _os == 0 ) { _os = &std::cout; } + + *_os << "\n0% 10 20 30 40 50 60 70 80 90 100%\n" + "|----|----|----|----|----|----|----|----|----|----|" << std::endl; + if ( !_expected_count ) _expected_count = 1; // prevent divide by zero + } // restart + +void progress_display::display_tic() { + + assert( _os != 0 ); + int tics_needed = (static_cast(_count)/_expected_count)*50.0; + do { *_os << '*' << std::flush; } while ( ++_tic < tics_needed ); + _next_tic_count = (_tic/50.0)*_expected_count; // use fp so large counts work + if ( _count == _expected_count ) { + if ( _tic < 51 ) *_os << '*'; + *_os << std::endl; + } + } // display_tic + +} // namespace boost + diff --git a/prg_timer.cpp b/prg_timer.cpp new file mode 100644 index 0000000..6425a9e --- /dev/null +++ b/prg_timer.cpp @@ -0,0 +1,40 @@ + // progress_timer class implementation ------------------------------------// + +// (C) Copyright Beman Dawes 1994-99. Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// Revision History +// 16 Jul 99 Second beta +// 6 Jul 99 Initial boost version + +#include +#include + +namespace boost { + +progress_timer::~progress_timer() { + +// Throwing an exception from a destructor is a Bad Thing. +// The progress_timer destructor does output which may throw. +// A progress_timer is usually not critical to the application. +// Therefore, wrap the I/O in a try block, catch and ignore all exceptions. + try { + + if( _os == 0 ) { _os = &std::cout; } + + std::ios_base::fmtflags old_flags = _os->setf( std::ios_base::fixed, + std::ios_base::floatfield ); + std::streamsize old_prec = _os->precision( 2 ); + *_os << elapsed() << " s\n" // "s" is System International d'Unités std + << std::endl; + _os->flags( old_flags ); + _os->precision( old_prec ); + } // try block + + catch (...) {} // eat any exceptions + + } // ~progress_timer + +} // namespace boost diff --git a/timer.cpp b/timer.cpp new file mode 100644 index 0000000..68bb29d --- /dev/null +++ b/timer.cpp @@ -0,0 +1,53 @@ +// timer class implementation ----------------------------------------------// + +// (C) Copyright Beman Dawes 1994-98. Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +/// Portability warning: this implementation depends on std::clock() returning +/// elapsed time, and CLOCKS_PER_SEC being reasonably large. Those +/// requirements are not met by all operating systems. + +// Revision History +// 25 Sep 99 elapsed_max() and elapsed_min() added (John_Maddock) +// 16 Jul 99 Second beta +// 6 Jul 99 Initial boost version + +#include +#include +#include + +namespace std {} /// cope with implementations which do not yet +using namespace std; /// place the functions in namespace std + /// Yes, this is ugly, but it is also the real world + +namespace boost { + +class timer::_timer { public: clock_t start_time; }; + +timer::timer() : _imp( new _timer ) { _imp->start_time = clock(); } + +timer::timer( const timer& src ) : _imp( new _timer ) + { _imp->start_time = src._imp->start_time; } +timer::~timer(){} + +void timer::restart() { _imp->start_time = clock(); } + +timer& timer::operator=( const timer& src ) + { _imp->start_time = src._imp->start_time; return * this; } + +double timer::elapsed() const + { return double(clock() - _imp->start_time) / CLOCKS_PER_SEC; } + +double timer::elapsed_max() const { + return (double(numeric_limits::max()) + - double(_imp->start_time)) / double(CLOCKS_PER_SEC); +} + +double timer::elapsed_min() const { return double(1)/double(CLOCKS_PER_SEC); } + + + +} // namespace boost + diff --git a/timer_test.cpp b/timer_test.cpp new file mode 100644 index 0000000..72fc05d --- /dev/null +++ b/timer_test.cpp @@ -0,0 +1,83 @@ +// timer, job_timer, and progress_display sample program -------------------// + +// (C) Copyright Beman Dawes 1998. Permission to copy, use, modify, sell and +// distribute this software is granted provided this copyright notice appears +// in all copies. This software is provided "as is" without express or implied +// warranty, and with no claim as to its suitability for any purpose. + +// Revision History +// 25 Sep 99 added elapsed_min() and elapsed_max() reporting +// 16 Jul 99 Second beta +// 6 Jul 99 Initial boost version + +#include +#include +#include + +using boost::timer; +using boost::progress_timer; +using boost::progress_display; +using std::cout; +using std::endl; + +int main() { + + timer t0; // used only for elapsed_max() and elapsed_min() + + cout << "timer::elapased_min() reports " << t0.elapsed_min() << " seconds\n"; + cout << "timer::elapased_max() reports " << t0.elapsed_max() + << " seconds, which is " << t0.elapsed_max()/3600.0 << " hours\n"; + + cout << "\nverify progress_display(0) doesn't divide by zero" << endl; + progress_display zero( 0 ); // verify 0 doesn't divide by zero + ++zero; + + long loops; + timer loop_timer; + const double time = 4.0; + + cout << "\ndetermine " << time << " second iteration count" << endl; + for ( loops = 0; loops < LONG_MAX + && loop_timer.elapsed() < time; ++loops ) {} + cout << loops << " iterations"<< endl; + + long i; + bool time_waster; // defeat [some] optimizers by storing result here + + progress_timer pt; + timer t1; + timer t4; + timer t5; + + cout << "\nburn about " << time << " seconds" << endl; + progress_display pd( loops ); + for ( i = loops; i--; ) + { time_waster = loop_timer.elapsed() < time; ++pd; } + + timer t2( t1 ); + timer t3; + t4 = t3; + t5.restart(); + + cout << "\nburn about " << time << " seconds again" << endl; + pd.restart( loops ); + for ( i = loops; i--; ) + { time_waster = loop_timer.elapsed() < time; ++pd; } + + cout << "t1 elapsed: " << t1.elapsed() << endl; + cout << "t2 elapsed: " << t2.elapsed() << endl; + cout << "t3 elapsed: " << t3.elapsed() << endl; + cout << "t4 elapsed: " << t4.elapsed() << endl; + cout << "t5 elapsed: " << t5.elapsed() << endl; + cout << "t1 and t2 should report the same times (very approximately " + << 2*time << " seconds)."<< endl; + cout << "t3, t4 and t5 should report the same times," << endl; + cout << "and these should be about half the t1 and t2 times." << endl; + cout << "The following elapsed time should be slightly greater than t1." + << endl; + return 0; + } // main + + + + From 6d7f6b95a3e26223d1bb4639d16230cbefd61d52 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Thu, 27 Jul 2000 14:27:00 +0000 Subject: [PATCH 2/2] Initial HTML commit [SVN r7640] --- index.htm | 43 ++++++++++++ timer.htm | 195 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100644 index.htm create mode 100644 timer.htm diff --git a/index.htm b/index.htm new file mode 100644 index 0000000..0666810 --- /dev/null +++ b/index.htm @@ -0,0 +1,43 @@ + + + + + + +Boost Timer Library + + + + + + + + + + + + + +
c++boost.gif (8819 bytes)Home Libraries People FAQ More
+ +

Timer library

+ +

The timer library supplies a timer class for measuring elapsed time, a +progress_timer class for reporting elapsed time, and a progress_display class +for displaying an indication of progress toward a goal. + +

+ +

Revised July 23, 1999

+ + + + diff --git a/timer.htm b/timer.htm new file mode 100644 index 0000000..88412d8 --- /dev/null +++ b/timer.htm @@ -0,0 +1,195 @@ + + + + + + + +Boost Timer Documentation + + + + +

c++boost.gif (8819 bytes) + Timer Library

+

The timer library provides two headers and three classes:

+ +
+ + + + + + + + + + + + + + + + + + + + + +
HeaderClassFunctionality
timer.hpptimerMeasure elapsed time.
progress.hppprogress_timerMeasure elapsed time (using timer), display on destruction.
progress.hppprogress_displayDisplay an indication of progress toward a known goal.
+
+

The objective in designing these classes was fairly limited - they are +intended for simple uses like timing and reporting progress for programmer's +tests or batch job streams. The specifications of the progress classes are +worded in very general terms to permit alternate implementations such as for +graphical user interfaces.

+

Class timer

+

Class timer measures elapsed time.  It is generally useful for minor +timing tasks.  Its supplied implementation offers moderate portability at +the cost of depending on the unknown accuracy and precision of the C Standard +Library clock() function.  The maximum measurable elapsed time may be as +low as 596.5 hours (or even less) for the supplied implementation. Because of +these limitations, this timer cannot be depended upon to +be robust, and should not be used if that is of any concern.

+

Synopsis

+
#include <boost/timer.hpp>
+namespace boost {
+class timer {
+ public:
+         timer();                        // postcondition: elapsed()==0
+         timer( const timer& src );      // post: elapsed()==src.elapsed()
+        ~timer();
+  timer& operator=( const timer& src );  // post: elapsed()==src.elapsed()
+  void   restart();                      // post: elapsed()==0
+  double elapsed() const;                // return elapsed time in seconds
+
+  double elapsed_max() const;  // return estimated maximum value for elapsed()
+  // Portability warning: elapsed_max() may return too high a value on systems
+  // where std::clock_t overflows or resets at surprising values.
+
+  double elapsed_min() const;            // return minimum value for elapsed()
+  }; // timer
+} // namespace boost
+

Exception safety

+

The constructors may throw std::bad_alloc.  No other member +functions throw exceptions.

+

Future directions

+

There was a very reasonable request from Ed Brey for a method of determining +the maximum value which may be returned by elapsed(), but there isn't a way to do so +portably. The issue has been raised with the group working on extended time functionality for the C language. A solution +may be years in the future. In the meantime, elapsed_max() provides an +approximation.

+

Class progress_timer

+

Class progress_timer automatically measures elapsed time, and then on +destruction displays an elapsed time message at an appropriate place in an appropriate form.  +The supplied implementation defaults to a character display on std::cout.

+

Class progress_timer is often used to time program execution.  Its use is as simple as:

+
+
#include <boost/progress.hpp>
+int main()
+{
+   progress_timer t;  // start timing
+   // do something ...
+   return 0;
+}
+
+

Which will produce some appropriate output, for example:

+
+
1.23 s
+
+

Note that "s" is the official System International d'Unités +abbreviation for seconds.

+

Synopsis

+
#include <boost/progress.hpp>
+namespace boost {
+class progress_timer : public timer, noncopyable  {
+ public:
+   progress_timer();
+   progress_timer( std::ostream& os ); // os is hint; implementation may ignore
+   ~progress_timer();
+   }; // progress_display
+} // namespace boost
+

Exception safety

+

The constructors may throw std::bad_alloc.  No other member +functions throw exceptions.

+

Class progress_display

+

Class progress_display displays an appropriate indication of progress toward +a predefined goal at an appropriate place in an appropriate form.  This +meets a human need to know if a program is progressing.

+

For example, if a lengthy computation must be done on a std::map<> +named big_map, the follow code would display an indication of progress:

+
  progress_display( big_map.size() ) show_progress;
+  for ( big_map_t::iterator itr = big_map:begin();
+        itr != big_map.end(); ++itr )
+  {
+     // do the computation
+     ...
+     ++show_progress;
+  }
+

After 70% of the elements have been processed, the display might look +something like this:

+
+
0%   10   20   30   40   50   60   70   80   90   100%
+|----|----|----|----|----|----|----|----|----|----|
+************************************
+
+ +

Synopsis

+
#include <boost/progress.hpp>
+namespace boost {
+class progress_display : noncopyable {
+ public:
+   progress_display( unsigned long expected_count );
+   // Effects: restart(expected_count)
+
+   progress_display( unsigned long expected_count,
+                     std::ostream& os ) // os is hint; implementation may ignore
+   // Effects: restart(expected_count)
+
+   void           restart( unsigned long expected_count );
+   //  Effects: display appropriate scale
+   //  Postconditions: count()==0, expected_count()==expected_count
+
+   unsigned long  operator+=( unsigned long increment )
+   //  Effects: Display appropriate progress tic if needed.
+   //  Postconditions: count()== original count() + increment
+   //  Returns: count().
+
+   unsigned long  operator++()
+   //  Returns: operator+=( 1 ).
+
+   unsigned long  count() const
+   //  Returns: The internal count.
+
+   unsigned long  expected_count() const
+   //  Returns: The expected_count from the constructor.
+
+   }; // progress_display
+} // namespace boost
+

Exception safety

+

All member functions except count() and expected_count() do output, and so in +theory may throw exceptions.  In practice it seems an exception being +thrown is pretty unlikely, and probably implies such serious problems that an +exception is warranted.  Note that there is no explicit destructor, so the +destructor throwing is not an issue.

+

History

+

These classes are descended from older C++ and C functionality found useful +by programmers for many years. Via the Boost mailing list, Reid Sweatman +suggested separating the more widely useful timer class from the more targeted +progress classes. Sean Corfield suggested allowing output to any ostream.  +Dave Abrahams, Valentin Bonnard, Ed Brey, Andy Glew, and Dietmar Kühl also +provided useful comments.  Ed Brey suggested timer::elapsed_max(). John +Maddock suggested timer::elapsed_min().

+
+

© Copyright Beman Dawes 1999. Permission to copy, use, modify, sell and +distribute this document is granted provided this copyright notice appears in +all copies. The software described is provided "as is" without express +or implied  warranty, and with no claim as to its suitability for any +purpose.

+ +

Revised May 05, 2000 +

+ + + +