Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / util / performance / time / timing_test_base.hpp
blob8e4cd99dc0e918424b521e9ad2ba61df9cd95fc0
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
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
8 // Foundation; either version 2, or (at your option) any later
9 // version.
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 COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
40 // warranty.
42 /**
43 * @file timing_test_base.hpp
44 * Contains a base class for timing tests.
47 #ifndef PB_DS_TIMING_TEST_BASE_HPP
48 #define PB_DS_TIMING_TEST_BASE_HPP
50 #include <performance/time/elapsed_timer.hpp>
51 #include <statistic/result_recorder.hpp>
53 namespace __gnu_pbds
55 namespace test
57 namespace detail
59 class timing_test_base
61 protected:
62 template<typename Functor>
63 double
64 operator()(Functor& fn);
66 static double
67 min_time_res();
69 private:
70 template<typename Functor>
71 std::size_t
72 get_min_resolution(Functor&);
74 template<typename Functor>
75 double
76 run_at_resolution(Functor&, std::size_t);
79 template<typename Functor>
80 double
81 timing_test_base::operator()(Functor& fn)
83 const std::size_t resolution = get_min_resolution(fn);
84 __gnu_pbds::test::detail::result_recorder<double> rec;
85 double res;
87 res = run_at_resolution(fn, resolution);
88 while (rec.add_result(res) == false);
89 res = rec.get_sample_mean() / resolution;
90 return res;
93 double
94 timing_test_base::min_time_res()
95 { return 1e-7; }
97 template<typename Functor>
98 std::size_t
99 timing_test_base::get_min_resolution(Functor& fn)
101 std::size_t guess = 0;
102 const double epsilon_val = min_time_res();
103 double res;
106 guess = guess * 2 + 1;
107 res = run_at_resolution(fn, guess);
109 while (res < epsilon_val);
110 return guess;
113 template<typename Functor>
114 double
115 timing_test_base::run_at_resolution(Functor& fn, std::size_t resolution)
117 elapsed_timer res;
118 fn(resolution);
119 return res;
122 } // namespace detail
123 } // namespace test
124 } // namespace __gnu_pbds
126 #endif