1 // boost timer.hpp header file ---------------------------------------------//
3 // (C) Copyright Beman Dawes 1994-99. Permission to copy, use, modify, sell and
4 // distribute this software is granted provided this copyright notice appears
5 // in all copies. This software is provided "as is" without express or implied
6 // warranty, and with no claim as to its suitability for any purpose.
8 // See http://www.boost.org for most recent version including documentation.
11 // 01 Apr 01 Modified to use new <boost/limits.hpp> header. (JMaddock)
12 // 12 Jan 01 Change to inline implementation to allow use without library
13 // builds. See docs for more rationale. (Beman Dawes)
14 // 25 Sep 99 elapsed_max() and elapsed_min() added (John Maddock)
15 // 16 Jul 99 Second beta
16 // 6 Jul 99 Initial boost version
18 #ifndef BOOST_TIMER_HPP
19 #define BOOST_TIMER_HPP
21 #include <boost/config.hpp>
23 #include <boost/limits.hpp>
25 # ifdef BOOST_NO_STDC_NAMESPACE
26 namespace std { using ::clock_t; using ::clock; }
32 // timer -------------------------------------------------------------------//
34 // A timer object measures elapsed time.
36 // It is recommended that implementations measure wall clock rather than CPU
37 // time since the intended use is performance measurement on systems where
38 // total elapsed time is more important than just process or CPU time.
40 // Warnings: The maximum measurable elapsed time may well be only 596.5+ hours
41 // due to implementation limitations. The accuracy of timings depends on the
42 // accuracy of timing information provided by the underlying platform, and
43 // this varies a great deal from platform to platform.
48 timer() { _start_time = std::clock(); } // postcondition: elapsed()==0
49 // timer( const timer& src ); // post: elapsed()==src.elapsed()
51 // timer& operator=( const timer& src ); // post: elapsed()==src.elapsed()
52 void restart() { _start_time = std::clock(); } // post: elapsed()==0
53 double elapsed() const // return elapsed time in seconds
54 { return double(std::clock() - _start_time) / CLOCKS_PER_SEC; }
56 double elapsed_max() const // return estimated maximum value for elapsed()
57 // Portability warning: elapsed_max() may return too high a value on systems
58 // where std::clock_t overflows or resets at surprising values.
60 return (double(std::numeric_limits<std::clock_t>::max())
61 - double(_start_time)) / double(CLOCKS_PER_SEC);
64 double elapsed_min() const // return minimum value for elapsed()
65 { return double(1)/double(CLOCKS_PER_SEC); }
68 std::clock_t _start_time;
73 #endif // BOOST_TIMER_HPP