]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/util/performance/time/timing_test_base.hpp
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / performance / time / timing_test_base.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
a945c346 3// Copyright (C) 2005-2024 Free Software Foundation, Inc.
4569a895
AT
4//
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
748086b7 8// Foundation; either version 3, or (at your option) any later
4569a895
AT
9// version.
10
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.
15
16// You should have received a copy of the GNU General Public License
748086b7
JJ
17// along with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
4569a895 19
4569a895
AT
20
21// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
22
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
30// warranty.
31
32/**
33 * @file timing_test_base.hpp
34 * Contains a base class for timing tests.
35 */
36
37#ifndef PB_DS_TIMING_TEST_BASE_HPP
38#define PB_DS_TIMING_TEST_BASE_HPP
39
40#include <performance/time/elapsed_timer.hpp>
382a1351 41#include <statistic/result_recorder.hpp>
4569a895 42
5e11f978 43namespace __gnu_pbds
4569a895
AT
44{
45 namespace test
46 {
47 namespace detail
48 {
49 class timing_test_base
50 {
51 protected:
52 template<typename Functor>
53 double
54 operator()(Functor& fn);
55
56 static double
57 min_time_res();
58
59 private:
60 template<typename Functor>
61 std::size_t
62 get_min_resolution(Functor&);
63
64 template<typename Functor>
65 double
66 run_at_resolution(Functor&, std::size_t);
67 };
68
69 template<typename Functor>
70 double
71 timing_test_base::operator()(Functor& fn)
72 {
73 const std::size_t resolution = get_min_resolution(fn);
5e11f978 74 __gnu_pbds::test::detail::result_recorder<double> rec;
4569a895
AT
75 double res;
76 do
77 res = run_at_resolution(fn, resolution);
382a1351
BK
78 while (rec.add_result(res) == false);
79 res = rec.get_sample_mean() / resolution;
4569a895
AT
80 return res;
81 }
82
83 double
84 timing_test_base::min_time_res()
85 { return 1e-7; }
86
87 template<typename Functor>
88 std::size_t
89 timing_test_base::get_min_resolution(Functor& fn)
90 {
91 std::size_t guess = 0;
92 const double epsilon_val = min_time_res();
93 double res;
94 do
95 {
96 guess = guess * 2 + 1;
97 res = run_at_resolution(fn, guess);
98 }
99 while (res < epsilon_val);
100 return guess;
101 }
102
103 template<typename Functor>
104 double
105 timing_test_base::run_at_resolution(Functor& fn, std::size_t resolution)
106 {
107 elapsed_timer res;
108 fn(resolution);
109 return res;
110 }
111
112 } // namespace detail
113 } // namespace test
5e11f978 114} // namespace __gnu_pbds
4569a895 115
f92ab29f 116#endif
4569a895 117