timer.hpp 4.6 KB

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