2
0
mirror of https://github.com/boostorg/timer.git synced 2026-01-19 04:42:13 +00:00

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]
This commit is contained in:
Beman Dawes
2000-07-07 16:04:40 +00:00
commit 7c8b8d52e0
7 changed files with 453 additions and 0 deletions

96
.gitattributes vendored Normal file
View File

@@ -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

View File

@@ -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 <boost/timer.hpp>
#include <boost/utility.hpp>
#include <iosfwd>
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

57
include/boost/timer.hpp Normal file
View File

@@ -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 <boost/smart_ptr.hpp>
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

43
prg_display.cpp Normal file
View File

@@ -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 <boost/progress.hpp>
#include <iostream>
#include <cassert>
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<double>(_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

40
prg_timer.cpp Normal file
View File

@@ -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 <boost/progress.hpp>
#include <iostream>
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

53
timer.cpp Normal file
View File

@@ -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 <boost/timer.hpp>
#include <ctime>
#include <limits>
namespace std {} /// cope with implementations which do not yet
using namespace std; /// place the <ctime> 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<clock_t>::max())
- double(_imp->start_time)) / double(CLOCKS_PER_SEC);
}
double timer::elapsed_min() const { return double(1)/double(CLOCKS_PER_SEC); }
} // namespace boost

83
timer_test.cpp Normal file
View File

@@ -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 <boost/progress.hpp>
#include <iostream>
#include <climits>
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