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