3 // Copyright (C) 2005-2021 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
23 // Permission to use, copy, modify, sell, and distribute this software
24 // is hereby granted without fee, provided that the above copyright
25 // notice appears in all copies, and that both that copyright notice
26 // and this permission notice appear in supporting documentation. None
27 // of the above authors, nor IBM Haifa Research Laboratories, make any
28 // representation about the suitability of this software for any
29 // purpose. It is provided "as is" without express or implied
33 * @file timing_test_base.hpp
34 * Contains a base class for timing tests.
37 #ifndef PB_DS_TIMING_TEST_BASE_HPP
38 #define PB_DS_TIMING_TEST_BASE_HPP
40 #include <performance/time/elapsed_timer.hpp>
41 #include <statistic/result_recorder.hpp>
49 class timing_test_base
52 template<typename Functor>
54 operator()(Functor& fn);
60 template<typename Functor>
62 get_min_resolution(Functor&);
64 template<typename Functor>
66 run_at_resolution(Functor&, std::size_t);
69 template<typename Functor>
71 timing_test_base::operator()(Functor& fn)
73 const std::size_t resolution = get_min_resolution(fn);
74 __gnu_pbds::test::detail::result_recorder<double> rec;
77 res = run_at_resolution(fn, resolution);
78 while (rec.add_result(res) == false);
79 res = rec.get_sample_mean() / resolution;
84 timing_test_base::min_time_res()
87 template<typename Functor>
89 timing_test_base::get_min_resolution(Functor& fn)
91 std::size_t guess = 0;
92 const double epsilon_val = min_time_res();
96 guess = guess * 2 + 1;
97 res = run_at_resolution(fn, guess);
99 while (res < epsilon_val);
103 template<typename Functor>
105 timing_test_base::run_at_resolution(Functor& fn, std::size_t resolution)
112 } // namespace detail
114 } // namespace __gnu_pbds