timer.hpp 4.5 KB

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