timer.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // boost timer.hpp header file ---------------------------------------------//
  2. // Copyright Beman Dawes 1994-99. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/timer for documentation.
  6. // Revision History
  7. // 01 Apr 01 Modified to use new <boost/limits.hpp> header. (JMaddock)
  8. // 12 Jan 01 Change to inline implementation to allow use without library
  9. // builds. See docs for more rationale. (Beman Dawes)
  10. // 25 Sep 99 elapsed_max() and elapsed_min() added (John Maddock)
  11. // 16 Jul 99 Second beta
  12. // 6 Jul 99 Initial boost version
  13. #ifndef BOOST_TIMER_HPP
  14. #define BOOST_TIMER_HPP
  15. #if !defined(BOOST_TIMER_ENABLE_DEPRECATED)
  16. # error This header is deprecated and will be removed. (You can define BOOST_TIMER_ENABLE_DEPRECATED to suppress this error.)
  17. #endif
  18. #include <boost/config/header_deprecated.hpp>
  19. BOOST_HEADER_DEPRECATED( "the facilities in <boost/timer/timer.hpp>" )
  20. #include <boost/config.hpp>
  21. #include <ctime>
  22. #include <boost/limits.hpp>
  23. # ifdef BOOST_NO_STDC_NAMESPACE
  24. namespace std { using ::clock_t; using ::clock; }
  25. # endif
  26. namespace boost {
  27. // timer -------------------------------------------------------------------//
  28. // A timer object measures elapsed time.
  29. // It is recommended that implementations measure wall clock rather than CPU
  30. // time since the intended use is performance measurement on systems where
  31. // total elapsed time is more important than just process or CPU time.
  32. // Warnings: The maximum measurable elapsed time may well be only 596.5+ hours
  33. // due to implementation limitations. The accuracy of timings depends on the
  34. // accuracy of timing information provided by the underlying platform, and
  35. // this varies a great deal from platform to platform.
  36. class timer
  37. {
  38. public:
  39. timer() { _start_time = std::clock(); } // postcondition: elapsed()==0
  40. // timer( const timer& src ); // post: elapsed()==src.elapsed()
  41. // ~timer(){}
  42. // timer& operator=( const timer& src ); // post: elapsed()==src.elapsed()
  43. void restart() { _start_time = std::clock(); } // post: elapsed()==0
  44. double elapsed() const // return elapsed time in seconds
  45. { return double(std::clock() - _start_time) / CLOCKS_PER_SEC; }
  46. double elapsed_max() const // return estimated maximum value for elapsed()
  47. // Portability warning: elapsed_max() may return too high a value on systems
  48. // where std::clock_t overflows or resets at surprising values.
  49. {
  50. return (double((std::numeric_limits<std::clock_t>::max)())
  51. - double(_start_time)) / double(CLOCKS_PER_SEC);
  52. }
  53. double elapsed_min() const // return minimum value for elapsed()
  54. { return double(1)/double(CLOCKS_PER_SEC); }
  55. private:
  56. std::clock_t _start_time;
  57. }; // timer
  58. } // namespace boost
  59. #endif // BOOST_TIMER_HPP