timer.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // boost/timer/timer.hpp -------------------------------------------------------------//
  2. // Copyright Beman Dawes 1994-2007, 2011
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. #ifndef BOOST_TIMER_TIMER_HPP
  6. #define BOOST_TIMER_TIMER_HPP
  7. #include <boost/timer/config.hpp>
  8. #include <boost/cstdint.hpp>
  9. #include <string>
  10. #include <cstring>
  11. #include <ostream>
  12. #include <boost/config/abi_prefix.hpp> // must be the last #include
  13. # if defined(_MSC_VER)
  14. # pragma warning(push) // Save warning settings
  15. # pragma warning(disable : 4251) // disable warning: class 'std::basic_string<_Elem,_Traits,_Ax>'
  16. # endif // needs to have dll-interface...
  17. //--------------------------------------------------------------------------------------//
  18. namespace boost
  19. {
  20. namespace timer
  21. {
  22. class cpu_timer;
  23. class auto_cpu_timer;
  24. typedef boost::int_least64_t nanosecond_type;
  25. struct cpu_times
  26. {
  27. nanosecond_type wall;
  28. nanosecond_type user;
  29. nanosecond_type system;
  30. void clear() { wall = user = system = 0; }
  31. };
  32. const short default_places = 6;
  33. BOOST_TIMER_DECL
  34. std::string format(const cpu_times& times, short places, const std::string& format);
  35. BOOST_TIMER_DECL
  36. std::string format(const cpu_times& times, short places = default_places);
  37. // cpu_timer -------------------------------------------------------------------------//
  38. class BOOST_TIMER_DECL cpu_timer
  39. {
  40. public:
  41. // constructor
  42. cpu_timer() BOOST_NOEXCEPT { start(); }
  43. // observers
  44. bool is_stopped() const BOOST_NOEXCEPT { return m_is_stopped; }
  45. cpu_times elapsed() const BOOST_NOEXCEPT; // does not stop()
  46. std::string format(short places, const std::string& format) const
  47. { return ::boost::timer::format(elapsed(), places, format); }
  48. std::string format(short places = default_places) const
  49. { return ::boost::timer::format(elapsed(), places); }
  50. // actions
  51. void start() BOOST_NOEXCEPT;
  52. void stop() BOOST_NOEXCEPT;
  53. void resume() BOOST_NOEXCEPT;
  54. private:
  55. cpu_times m_times;
  56. bool m_is_stopped;
  57. };
  58. // auto_cpu_timer --------------------------------------------------------------------//
  59. class BOOST_TIMER_DECL auto_cpu_timer : public cpu_timer
  60. {
  61. public:
  62. // Explicit defaults for os are not provided to avoid including <iostream>, which has
  63. // high costs even when the standard streams are not actually used. Explicit defaults
  64. // for format are not provided to avoid order-of-dynamic-initialization issues with a
  65. // std::string.
  66. explicit auto_cpu_timer(short places = default_places); // #1
  67. auto_cpu_timer(short places, const std::string& format); // #2
  68. explicit auto_cpu_timer(const std::string& format); // #3
  69. auto_cpu_timer(std::ostream& os, short places,
  70. const std::string& format) // #4
  71. : m_places(places), m_os(&os), m_format(format)
  72. { start(); }
  73. explicit auto_cpu_timer(std::ostream& os, short places = default_places); // #5
  74. auto_cpu_timer(std::ostream& os, const std::string& format) // #6
  75. : m_places(default_places), m_os(&os), m_format(format)
  76. { start(); }
  77. ~auto_cpu_timer();
  78. // observers
  79. // not particularly useful to users, but allow testing of constructor
  80. // postconditions and ease specification of other functionality without resorting
  81. // to "for exposition only" private members.
  82. std::ostream& ostream() const { return *m_os; }
  83. short places() const { return m_places; }
  84. const std::string& format_string() const { return m_format; }
  85. // actions
  86. void report();
  87. private:
  88. short m_places;
  89. std::ostream* m_os; // stored as ptr so compiler can generate operator=
  90. std::string m_format;
  91. };
  92. } // namespace timer
  93. } // namespace boost
  94. # if defined(_MSC_VER)
  95. # pragma warning(pop) // restore warning settings.
  96. # endif
  97. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  98. #endif // BOOST_TIMER_TIMER_HPP