Chris@16: // boost/timer/timer.hpp -------------------------------------------------------------// Chris@16: Chris@16: // Copyright Beman Dawes 1994-2007, 2011 Chris@16: Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // See http://www.boost.org/LICENSE_1_0.txt Chris@16: Chris@16: #ifndef BOOST_TIMER_TIMER_HPP Chris@16: #define BOOST_TIMER_TIMER_HPP Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include // must be the last #include Chris@16: Chris@16: # if defined(_MSC_VER) Chris@16: # pragma warning(push) // Save warning settings Chris@16: # pragma warning(disable : 4251) // disable warning: class 'std::basic_string<_Elem,_Traits,_Ax>' Chris@16: # endif // needs to have dll-interface... Chris@16: Chris@16: //--------------------------------------------------------------------------------------// Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace timer Chris@16: { Chris@16: class cpu_timer; Chris@16: class auto_cpu_timer; Chris@16: Chris@16: typedef boost::int_least64_t nanosecond_type; Chris@16: Chris@16: struct cpu_times Chris@16: { Chris@16: nanosecond_type wall; Chris@16: nanosecond_type user; Chris@16: nanosecond_type system; Chris@16: Chris@16: void clear() { wall = user = system = 0LL; } Chris@16: }; Chris@16: Chris@16: const short default_places = 6; Chris@16: Chris@16: BOOST_TIMER_DECL Chris@16: std::string format(const cpu_times& times, short places, const std::string& format); Chris@16: Chris@16: BOOST_TIMER_DECL Chris@16: std::string format(const cpu_times& times, short places = default_places); Chris@16: Chris@16: // cpu_timer -------------------------------------------------------------------------// Chris@16: Chris@16: class BOOST_TIMER_DECL cpu_timer Chris@16: { Chris@16: public: Chris@16: Chris@16: // constructor Chris@101: cpu_timer() BOOST_NOEXCEPT { start(); } Chris@16: Chris@16: // observers Chris@101: bool is_stopped() const BOOST_NOEXCEPT { return m_is_stopped; } Chris@101: cpu_times elapsed() const BOOST_NOEXCEPT; // does not stop() Chris@16: std::string format(short places, const std::string& format) const Chris@16: { return ::boost::timer::format(elapsed(), places, format); } Chris@16: std::string format(short places = default_places) const Chris@16: { return ::boost::timer::format(elapsed(), places); } Chris@16: // actions Chris@101: void start() BOOST_NOEXCEPT; Chris@101: void stop() BOOST_NOEXCEPT; Chris@101: void resume() BOOST_NOEXCEPT; Chris@16: Chris@16: private: Chris@16: cpu_times m_times; Chris@16: bool m_is_stopped; Chris@16: }; Chris@16: Chris@16: // auto_cpu_timer --------------------------------------------------------------------// Chris@16: Chris@16: class BOOST_TIMER_DECL auto_cpu_timer : public cpu_timer Chris@16: { Chris@16: public: Chris@16: Chris@16: // Explicit defaults for os are not provided to avoid including , which has Chris@16: // high costs even when the standard streams are not actually used. Explicit defaults Chris@16: // for format are not provided to avoid order-of-dynamic-initialization issues with a Chris@16: // std::string. Chris@16: Chris@16: explicit auto_cpu_timer(short places = default_places); // #1 Chris@16: auto_cpu_timer(short places, const std::string& format); // #2 Chris@16: explicit auto_cpu_timer(const std::string& format); // #3 Chris@16: auto_cpu_timer(std::ostream& os, short places, Chris@16: const std::string& format) // #4 Chris@16: : m_places(places), m_os(&os), m_format(format) Chris@16: { start(); } Chris@16: explicit auto_cpu_timer(std::ostream& os, short places = default_places); // #5 Chris@16: auto_cpu_timer(std::ostream& os, const std::string& format) // #6 Chris@16: : m_places(default_places), m_os(&os), m_format(format) Chris@16: { start(); } Chris@16: Chris@16: ~auto_cpu_timer(); Chris@16: Chris@16: // observers Chris@16: // not particularly useful to users, but allow testing of constructor Chris@16: // postconditions and ease specification of other functionality without resorting Chris@16: // to "for exposition only" private members. Chris@16: std::ostream& ostream() const { return *m_os; } Chris@16: short places() const { return m_places; } Chris@16: const std::string& format_string() const { return m_format; } Chris@16: Chris@16: // actions Chris@16: void report(); Chris@16: Chris@16: private: Chris@16: short m_places; Chris@16: std::ostream* m_os; // stored as ptr so compiler can generate operator= Chris@16: std::string m_format; Chris@16: }; Chris@16: Chris@16: } // namespace timer Chris@16: } // namespace boost Chris@16: Chris@16: # if defined(_MSC_VER) Chris@16: # pragma warning(pop) // restore warning settings. Chris@16: # endif Chris@16: Chris@16: #include // pops abi_prefix.hpp pragmas Chris@16: Chris@16: #endif // BOOST_TIMER_TIMER_HPP