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 // TODO:
|
Chris@16
|
30 //
|
Chris@16
|
31 // * Add BOOST_NOEXCEPT where applicable
|
Chris@16
|
32
|
Chris@16
|
33 //--------------------------------------------------------------------------------------//
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost
|
Chris@16
|
36 {
|
Chris@16
|
37 namespace timer
|
Chris@16
|
38 {
|
Chris@16
|
39 class cpu_timer;
|
Chris@16
|
40 class auto_cpu_timer;
|
Chris@16
|
41
|
Chris@16
|
42 typedef boost::int_least64_t nanosecond_type;
|
Chris@16
|
43
|
Chris@16
|
44 struct cpu_times
|
Chris@16
|
45 {
|
Chris@16
|
46 nanosecond_type wall;
|
Chris@16
|
47 nanosecond_type user;
|
Chris@16
|
48 nanosecond_type system;
|
Chris@16
|
49
|
Chris@16
|
50 void clear() { wall = user = system = 0LL; }
|
Chris@16
|
51 };
|
Chris@16
|
52
|
Chris@16
|
53 const short default_places = 6;
|
Chris@16
|
54
|
Chris@16
|
55 BOOST_TIMER_DECL
|
Chris@16
|
56 std::string format(const cpu_times& times, short places, const std::string& format);
|
Chris@16
|
57
|
Chris@16
|
58 BOOST_TIMER_DECL
|
Chris@16
|
59 std::string format(const cpu_times& times, short places = default_places);
|
Chris@16
|
60
|
Chris@16
|
61 // cpu_timer -------------------------------------------------------------------------//
|
Chris@16
|
62
|
Chris@16
|
63 class BOOST_TIMER_DECL cpu_timer
|
Chris@16
|
64 {
|
Chris@16
|
65 public:
|
Chris@16
|
66
|
Chris@16
|
67 // constructor
|
Chris@16
|
68 cpu_timer() { start(); }
|
Chris@16
|
69
|
Chris@16
|
70 // observers
|
Chris@16
|
71 bool is_stopped() const { return m_is_stopped; }
|
Chris@16
|
72 cpu_times elapsed() const; // does not stop()
|
Chris@16
|
73 std::string format(short places, const std::string& format) const
|
Chris@16
|
74 { return ::boost::timer::format(elapsed(), places, format); }
|
Chris@16
|
75 std::string format(short places = default_places) const
|
Chris@16
|
76 { return ::boost::timer::format(elapsed(), places); }
|
Chris@16
|
77 // actions
|
Chris@16
|
78 void start();
|
Chris@16
|
79 void stop();
|
Chris@16
|
80 void resume();
|
Chris@16
|
81
|
Chris@16
|
82 private:
|
Chris@16
|
83 cpu_times m_times;
|
Chris@16
|
84 bool m_is_stopped;
|
Chris@16
|
85 };
|
Chris@16
|
86
|
Chris@16
|
87 // auto_cpu_timer --------------------------------------------------------------------//
|
Chris@16
|
88
|
Chris@16
|
89 class BOOST_TIMER_DECL auto_cpu_timer : public cpu_timer
|
Chris@16
|
90 {
|
Chris@16
|
91 public:
|
Chris@16
|
92
|
Chris@16
|
93 // Explicit defaults for os are not provided to avoid including <iostream>, which has
|
Chris@16
|
94 // high costs even when the standard streams are not actually used. Explicit defaults
|
Chris@16
|
95 // for format are not provided to avoid order-of-dynamic-initialization issues with a
|
Chris@16
|
96 // std::string.
|
Chris@16
|
97
|
Chris@16
|
98 explicit auto_cpu_timer(short places = default_places); // #1
|
Chris@16
|
99 auto_cpu_timer(short places, const std::string& format); // #2
|
Chris@16
|
100 explicit auto_cpu_timer(const std::string& format); // #3
|
Chris@16
|
101 auto_cpu_timer(std::ostream& os, short places,
|
Chris@16
|
102 const std::string& format) // #4
|
Chris@16
|
103 : m_places(places), m_os(&os), m_format(format)
|
Chris@16
|
104 { start(); }
|
Chris@16
|
105 explicit auto_cpu_timer(std::ostream& os, short places = default_places); // #5
|
Chris@16
|
106 auto_cpu_timer(std::ostream& os, const std::string& format) // #6
|
Chris@16
|
107 : m_places(default_places), m_os(&os), m_format(format)
|
Chris@16
|
108 { start(); }
|
Chris@16
|
109
|
Chris@16
|
110 ~auto_cpu_timer();
|
Chris@16
|
111
|
Chris@16
|
112 // observers
|
Chris@16
|
113 // not particularly useful to users, but allow testing of constructor
|
Chris@16
|
114 // postconditions and ease specification of other functionality without resorting
|
Chris@16
|
115 // to "for exposition only" private members.
|
Chris@16
|
116 std::ostream& ostream() const { return *m_os; }
|
Chris@16
|
117 short places() const { return m_places; }
|
Chris@16
|
118 const std::string& format_string() const { return m_format; }
|
Chris@16
|
119
|
Chris@16
|
120 // actions
|
Chris@16
|
121 void report();
|
Chris@16
|
122
|
Chris@16
|
123 private:
|
Chris@16
|
124 short m_places;
|
Chris@16
|
125 std::ostream* m_os; // stored as ptr so compiler can generate operator=
|
Chris@16
|
126 std::string m_format;
|
Chris@16
|
127 };
|
Chris@16
|
128
|
Chris@16
|
129 } // namespace timer
|
Chris@16
|
130 } // namespace boost
|
Chris@16
|
131
|
Chris@16
|
132 # if defined(_MSC_VER)
|
Chris@16
|
133 # pragma warning(pop) // restore warning settings.
|
Chris@16
|
134 # endif
|
Chris@16
|
135
|
Chris@16
|
136 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
|
Chris@16
|
137
|
Chris@16
|
138 #endif // BOOST_TIMER_TIMER_HPP
|