1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2019 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 3, or (at your option) any later
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
17 // along with this library; see the file COPYING3.  If not see
18 // <http://www.gnu.org/licenses/>.
19 
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>
41 #include <statistic/result_recorder.hpp>
42 
43 namespace __gnu_pbds
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
operator ()(Functor & fn)71       timing_test_base::operator()(Functor& fn)
72       {
73 	const std::size_t resolution = get_min_resolution(fn);
74 	__gnu_pbds::test::detail::result_recorder<double> rec;
75 	double res;
76 	do
77 	  res = run_at_resolution(fn, resolution);
78 	while (rec.add_result(res) == false);
79 	res = rec.get_sample_mean() / resolution;
80 	return res;
81       }
82 
83       double
min_time_res()84       timing_test_base::min_time_res()
85       { return 1e-7; }
86 
87       template<typename Functor>
88       std::size_t
get_min_resolution(Functor & fn)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
run_at_resolution(Functor & fn,std::size_t resolution)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
114 } // namespace __gnu_pbds
115 
116 #endif
117 
118