]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/timer.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / multiprecision / test / timer.hpp
1 ///////////////////////////////////////////////////////////////
2 // Copyright Beman Dawes 1994-99.
3 // Copyright 2020 John Maddock.
4 // Copyright 2022 Christopher Kormanyos.
5 // Distributed under the Boost
6 // Software License, Version 1.0. (See accompanying file
7 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
8
9 #ifndef BOOST_MP_TIMER_HPP
10 #define BOOST_MP_TIMER_HPP
11
12 #include <chrono>
13
14 // This file now reflects the 2022 work-over. We have replaced the
15 // original timer facility (which had been based on std::clock())
16 // with C++11's equivalent <chrono> counterparts.
17
18 namespace boost { namespace multiprecision { namespace test_detail {
19
20 template <class ClockType = std::chrono::high_resolution_clock>
21 struct stopwatch;
22
23 template <class ResultType,
24 class ClockType = std::chrono::high_resolution_clock>
25 class timer_template;
26
27 template <class ClockType>
28 struct stopwatch
29 {
30 private:
31 using clock_type = ClockType;
32
33 public:
34 using duration_type = typename clock_type::duration;
35
36 stopwatch() : m_start(clock_type::now()) { }
37
38 stopwatch(const stopwatch& other) : m_start(other.m_start) { }
39 stopwatch(stopwatch&& other) noexcept : m_start(other.m_start) { }
40
41 auto operator=(const stopwatch& other) -> stopwatch&
42 {
43 if(this != &other)
44 {
45 m_start = other.m_start;
46 }
47
48 return *this;
49 }
50
51 auto operator=(stopwatch&& other) noexcept -> stopwatch&
52 {
53 m_start = other.m_start;
54
55 return *this;
56 }
57
58 ~stopwatch() { }
59
60 auto elapsed() const -> duration_type
61 {
62 return (clock_type::now() - m_start);
63 }
64
65 auto elapsed_max() const -> duration_type
66 {
67 return (std::chrono::time_point<clock_type>::max)() - m_start;
68 }
69
70 static constexpr auto elapsed_min() -> duration_type
71 {
72 return (std::chrono::time_point<clock_type>::min)() - std::chrono::time_point<clock_type>();
73 }
74
75 void reset()
76 {
77 m_start = clock_type::now();
78 }
79
80 private:
81 typename clock_type::time_point m_start;
82 };
83
84
85 template <class ResultType,
86 class ClockType>
87 class timer_template : public stopwatch<ClockType>
88 {
89 private:
90 using clock_type = ClockType;
91 using base_class_type = stopwatch<clock_type>;
92
93 public:
94 using result_type = ResultType;
95
96 timer_template() { }
97
98 ~timer_template() { }
99
100 void restart() { base_class_type::reset(); }
101
102 auto elapsed() const -> result_type
103 {
104 // Return the elapsed time in seconds.
105 return std::chrono::duration_cast<std::chrono::duration<result_type>>(base_class_type::elapsed()).count();
106 }
107
108 auto elapsed_max() const -> result_type
109 {
110 return std::chrono::duration_cast<std::chrono::duration<result_type>>(base_class_type::elapsed_max()).count();
111 }
112
113 static constexpr auto elapsed_min() -> result_type
114 {
115 return std::chrono::duration_cast<std::chrono::duration<result_type>>(base_class_type::elapsed_min()).count();
116 }
117
118 static constexpr result_type seconds(result_type s) { return static_cast<result_type>(s * static_cast<result_type>(1)); }
119
120 };
121
122 } // namespace test_detail
123 } // namespace multiprecision
124 } // namespace boost
125
126 // TODO: Might prefer to have the relatively common
127 // name "timer" to be shielded with a namespace.
128
129 using timer = boost::multiprecision::test_detail::timer_template<double, std::chrono::high_resolution_clock>;
130
131 #endif // BOOST_MP_TIMER_HPP