timer.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // boost/chrono/timer.hpp ------------------------------------------------------------//
  2. // Copyright Beman Dawes 2008
  3. // Copyright 2009 Vicente J. Botet Escriba
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/system for documentation.
  7. #ifndef BOOSTEX_CHRONO_TIMER_HPP
  8. #define BOOSTEX_CHRONO_TIMER_HPP
  9. #include <boost/chrono/chrono.hpp>
  10. #include <boost/system/error_code.hpp>
  11. namespace boost_ex
  12. {
  13. namespace chrono
  14. {
  15. //--------------------------------------------------------------------------------------//
  16. // timer //
  17. //--------------------------------------------------------------------------------------//
  18. template <class Clock=boost::chrono::high_resolution_clock>
  19. class timer
  20. {
  21. public:
  22. typedef Clock clock;
  23. typedef typename Clock::duration duration;
  24. typedef typename Clock::time_point time_point;
  25. explicit timer( boost::system::error_code & ec = ::boost::throws() )
  26. {
  27. start(ec);
  28. }
  29. ~timer() {} // never throws
  30. void start( boost::system::error_code & ec = ::boost::throws() )
  31. {
  32. m_start = clock::now( ec );
  33. }
  34. duration elapsed( boost::system::error_code & ec = ::boost::throws() )
  35. { return clock::now( ec ) - m_start; }
  36. private:
  37. time_point m_start;
  38. };
  39. typedef chrono::timer< boost::chrono::system_clock > system_timer;
  40. #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  41. typedef chrono::timer< boost::chrono::steady_clock > steady_timer;
  42. #endif
  43. typedef chrono::timer< boost::chrono::high_resolution_clock > high_resolution_timer;
  44. } // namespace chrono
  45. } // namespace boost_ex
  46. #endif