timer.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include <boost/config/header_deprecated.hpp>
  16. BOOST_HEADER_DEPRECATED( "the facilities in <boost/timer/timer.hpp>" )
  17. #include <boost/config.hpp>
  18. #include <ctime>
  19. #include <boost/limits.hpp>
  20. # ifdef BOOST_NO_STDC_NAMESPACE
  21. namespace std { using ::clock_t; using ::clock; }
  22. # endif
  23. namespace boost {
  24. // timer -------------------------------------------------------------------//
  25. // A timer object measures elapsed time.
  26. // It is recommended that implementations measure wall clock rather than CPU
  27. // time since the intended use is performance measurement on systems where
  28. // total elapsed time is more important than just process or CPU time.
  29. // Warnings: The maximum measurable elapsed time may well be only 596.5+ hours
  30. // due to implementation limitations. The accuracy of timings depends on the
  31. // accuracy of timing information provided by the underlying platform, and
  32. // this varies a great deal from platform to platform.
  33. class timer
  34. {
  35. public:
  36. timer() { _start_time = std::clock(); } // postcondition: elapsed()==0
  37. // timer( const timer& src ); // post: elapsed()==src.elapsed()
  38. // ~timer(){}
  39. // timer& operator=( const timer& src ); // post: elapsed()==src.elapsed()
  40. void restart() { _start_time = std::clock(); } // post: elapsed()==0
  41. double elapsed() const // return elapsed time in seconds
  42. { return double(std::clock() - _start_time) / CLOCKS_PER_SEC; }
  43. double elapsed_max() const // return estimated maximum value for elapsed()
  44. // Portability warning: elapsed_max() may return too high a value on systems
  45. // where std::clock_t overflows or resets at surprising values.
  46. {
  47. return (double((std::numeric_limits<std::clock_t>::max)())
  48. - double(_start_time)) / double(CLOCKS_PER_SEC);
  49. }
  50. double elapsed_min() const // return minimum value for elapsed()
  51. { return double(1)/double(CLOCKS_PER_SEC); }
  52. private:
  53. std::clock_t _start_time;
  54. }; // timer
  55. } // namespace boost
  56. #endif // BOOST_TIMER_HPP