|
Boost : |
From: Toon Knapen (toon.knapen_at_[hidden])
Date: 2001-10-31 10:54:05
I need some lightweight timer to calculate the total time a program
spends in some function (as it's a template function, `gprof`
incorporates it in the caller)
So I took a look at boost.timer. A few questions/remarks :
The comments in timer.hpp state : "It is recommended that
implementations measure wall clock rather than CPU time since ..."
`clock()` however returns process-time (CPU time) and not wall clock
time ? (although this is exactly what I wanted)
I need to be able to toggle the timer on and off. Since I want to
calculate the total time spend in some function I need to toggle the
timer off everytime the program leaves the function and back on
everytime the program enters the function again.
So I propose to add a member called `toggle()`.
A drawback though is that you need to `toggle` the timer back on before
asking the `elapsed()` time (otherwise I would need a bit to indicate if
the timer is on or off which increases the memory footprint. Maybe I
could use the MSB of the clock_t as a hack)
In attachment you can find my proposal implementation (Oh, I also
aligned with the coding guidelines AFAIK : trailing underscore for
members, ... )
toon
// 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
// 01 Apr 01 Modified to use new <boost/limits.hpp> header. (JMaddock)
// 12 Jan 01 Change to inline implementation to allow use without library
// builds. See docs for more rationale. (Beman Dawes)
// 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/config.hpp>
#include <ctime>
#include <boost/limits.hpp>
# ifdef BOOST_NO_STDC_NAMESPACE
namespace std { using ::clock_t; using ::clock; }
# endif
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:
/// create new timer
/// postcondition: elapsed()==0
timer()
: start_time_( std::clock() )
{
}
/// default copy constructor
/// postcondition: elapsed() == src.elapsed()
// timer( const timer& src );
/// default destructor
// ~timer(){}
/// default assignment
/// postcondition: elapsed()==src.elapsed()
// timer& operator=( const timer& src );
/// toggle the timer off and back on
void toggle()
{
start_time_ = std::clock() - start_time_;
}
/// postcondition: elapsed()==0
void restart()
{
start_time_ = std::clock();
}
/// return elapsed time in seconds
/// precondition: timer should be toggled on
double elapsed() const
{
return double( std::clock() - start_time_ ) / CLOCKS_PER_SEC;
}
/// return estimated maximum value for elapsed()
double elapsed_max() const
// Portability warning: elapsed_max() may return too high a value on systems
// where std::clock_t overflows or resets at surprising values.
{
return (double(std::numeric_limits<std::clock_t>::max())
- double(start_time_)) / double(CLOCKS_PER_SEC);
}
// return minimum value for elapsed()
double elapsed_min() const
{
return double(1)/double(CLOCKS_PER_SEC);
}
private:
std::clock_t start_time_;
}; // timer
} // namespace boost
#endif // BOOST_TIMER_HPP
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk