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
9 #ifndef BOOST_MP_TIMER_HPP
10 #define BOOST_MP_TIMER_HPP
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.
18 namespace boost { namespace multiprecision { namespace test_detail {
20 template <class ClockType = std::chrono::high_resolution_clock>
23 template <class ResultType,
24 class ClockType = std::chrono::high_resolution_clock>
27 template <class ClockType>
31 using clock_type = ClockType;
34 using duration_type = typename clock_type::duration;
36 stopwatch() : m_start(clock_type::now()) { }
38 stopwatch(const stopwatch& other) : m_start(other.m_start) { }
39 stopwatch(stopwatch&& other) noexcept : m_start(other.m_start) { }
41 auto operator=(const stopwatch& other) -> stopwatch&
45 m_start = other.m_start;
51 auto operator=(stopwatch&& other) noexcept -> stopwatch&
53 m_start = other.m_start;
60 auto elapsed() const -> duration_type
62 return (clock_type::now() - m_start);
65 auto elapsed_max() const -> duration_type
67 return (std::chrono::time_point<clock_type>::max)() - m_start;
70 static constexpr auto elapsed_min() -> duration_type
72 return (std::chrono::time_point<clock_type>::min)() - std::chrono::time_point<clock_type>();
77 m_start = clock_type::now();
81 typename clock_type::time_point m_start;
85 template <class ResultType,
87 class timer_template : public stopwatch<ClockType>
90 using clock_type = ClockType;
91 using base_class_type = stopwatch<clock_type>;
94 using result_type = ResultType;
100 void restart() { base_class_type::reset(); }
102 auto elapsed() const -> result_type
104 // Return the elapsed time in seconds.
105 return std::chrono::duration_cast<std::chrono::duration<result_type>>(base_class_type::elapsed()).count();
108 auto elapsed_max() const -> result_type
110 return std::chrono::duration_cast<std::chrono::duration<result_type>>(base_class_type::elapsed_max()).count();
113 static constexpr auto elapsed_min() -> result_type
115 return std::chrono::duration_cast<std::chrono::duration<result_type>>(base_class_type::elapsed_min()).count();
118 static constexpr result_type seconds(result_type s) { return static_cast<result_type>(s * static_cast<result_type>(1)); }
122 } // namespace test_detail
123 } // namespace multiprecision
126 // TODO: Might prefer to have the relatively common
127 // name "timer" to be shielded with a namespace.
129 using timer = boost::multiprecision::test_detail::timer_template<double, std::chrono::high_resolution_clock>;
131 #endif // BOOST_MP_TIMER_HPP