timer.hpp 2.6 KB

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