CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
time_test.hpp
Go to the documentation of this file.
1 // $Id: time_test.hpp 3855 2016-12-19 00:30:54Z bradbell $
2 # ifndef CPPAD_UTILITY_TIME_TEST_HPP
3 # define CPPAD_UTILITY_TIME_TEST_HPP
4 
5 /* --------------------------------------------------------------------------
6 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-16 Bradley M. Bell
7 
8 CppAD is distributed under multiple licenses. This distribution is under
9 the terms of the
10  Eclipse Public License Version 1.0.
11 
12 A copy of this license is included in the COPYING file of this distribution.
13 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
14 -------------------------------------------------------------------------- */
15 
16 /*
17 $begin time_test$$
18 $spell
19  gettimeofday
20  vec
21  cppad.hpp
22  Microsoft
23  namespace
24  std
25  const
26  cout
27  ctime
28  ifdef
29  const
30  endif
31  cpp
32 $$
33 
34 
35 $section Determine Amount of Time to Execute a Test$$
36 $mindex time_test speed$$
37 
38 $head Syntax$$
39 $codei%# include <cppad/utility/time_test.hpp>
40 %$$
41 $icode%time% = time_test(%test%, %time_min%)
42 %$$
43 $icode%time% = time_test(%test%, %time_min%, %test_size%)%$$
44 
45 $head Purpose$$
46 The $code time_test$$ function executes a timing test
47 and reports the amount of wall clock time for execution.
48 
49 $head Motivation$$
50 It is important to separate small calculation units
51 and test them individually.
52 This way individual changes can be tested in the context of the
53 routine that they are in.
54 On many machines, accurate timing of a very short execution
55 sequences is not possible.
56 In addition,
57 there may be set up and tear down time for a test that
58 we do not really want included in the timing.
59 For this reason $code time_test$$
60 automatically determines how many times to
61 repeat the section of the test that we wish to time.
62 
63 $head Include$$
64 The file $code cppad/time_test.hpp$$ defines the
65 $code time_test$$ function.
66 This file is included by $code cppad/cppad.hpp$$
67 and it can also be included separately with out the rest of
68 the $code CppAD$$ routines.
69 
70 $head test$$
71 The $code time_test$$ argument $icode test$$ is a function,
72 or function object.
73 In the case where $icode test_size$$ is not present,
74 $icode test$$ supports the syntax
75 $codei%
76  %test%(%repeat%)
77 %$$
78 In the case where $icode test_size$$ is present,
79 $icode test$$ supports the syntax
80 $codei%
81  %test%(%size%, %repeat%)
82 %$$
83 In either case, the return value for $icode test$$ is $code void$$.
84 
85 $subhead size$$
86 If the argument $icode size$$ is present,
87 it has prototype
88 $codei%
89  size_t %size%
90 %$$
91 and is equal to the $icode test_size$$ argument to $code time_test$$.
92 
93 $subhead repeat$$
94 The $icode test$$ argument $icode repeat$$ has prototype
95 $codei%
96  size_t %repeat%
97 %$$
98 It will be equal to the $icode size$$ argument to $code time_test$$.
99 
100 $head time_min$$
101 The argument $icode time_min$$ has prototype
102 $codei%
103  double %time_min%
104 %$$
105 It specifies the minimum amount of time in seconds
106 that the $icode test$$ routine should take.
107 The $icode repeat$$ argument to $icode test$$ is increased
108 until this amount of execution time (or more) is reached.
109 
110 $head test_size$$
111 This argument has prototype
112 $codei%
113  size_t %test_size%
114 %$$
115 It specifies the $icode size$$ argument to $icode test$$.
116 
117 $head time$$
118 The return value $icode time$$ has prototype
119 $codei%
120  double %time%
121 %$$
122 and is the number of wall clock seconds that it took
123 to execute $icode test$$ divided by the value used for $icode repeat$$.
124 
125 $head Timing$$
126 The routine $cref elapsed_seconds$$ will be used to determine the
127 amount of time it took to execute the test.
128 
129 $children%
130  cppad/utility/elapsed_seconds.hpp%
131  speed/example/time_test.cpp
132 %$$
133 $head Example$$
134 The routine $cref time_test.cpp$$ is an example and test
135 of $code time_test$$.
136 
137 $end
138 -----------------------------------------------------------------------
139 */
140 
141 # include <algorithm>
142 # include <cstddef>
143 # include <cmath>
145 # include <cppad/core/define.hpp>
146 
147 # define CPPAD_EXTRA_RUN_BEFORE_TIMING 0
148 
149 namespace CppAD { // BEGIN_CPPAD_NAMESPACE
150 /*!
151 \file time_test.hpp
152 \brief Function that preforms one timing test (for speed of execution).
153 */
154 
155 /*!
156 Preform one wall clock execution timing test.
157 
158 \tparam Test
159 Either the type <code>void (*)(size_t)</code> or a function object
160 type that supports the same syntax.
161 
162 \param test
163 The function, or function object, that supports the operation
164 <code>test(repeat)</code> where \c repeat is the number of times
165 to repeat the tests operaiton that is being timed.
166 
167 \param time_min
168 is the minimum amount of time that \c test should take to preform
169 the repetitions of the operation being timed.
170 */
171 template <class Test>
172 double time_test(Test test, double time_min )
173 {
174 # if CPPAD_EXTRA_RUN_BEFORE_TIMING
175  test(1);
176 # endif
177  size_t repeat = 0;
178  double s0 = elapsed_seconds();
179  double s1 = s0;
180  while( s1 - s0 < time_min )
181  { repeat = std::max(size_t(1), 2 * repeat);
182  s0 = elapsed_seconds();
183  test(repeat);
184  s1 = elapsed_seconds();
185  }
186  double time = (s1 - s0) / double(repeat);
187  return time;
188 }
189 
190 /*!
191 Preform one wall clock execution timing test.
192 
193 \tparam Test
194 Either the type <code>void (*)(size_t, size_t)</code> or a function object
195 type that supports the same syntax.
196 
197 \param test
198 The function, or function object, that supports the operation
199 <code>test(size, repeat)</code> where
200 \c is the size for this test and
201 \c repeat is the number of times
202 to repeat the tests operaiton that is being timed.
203 
204 \param time_min
205 is the minimum amount of time that \c test should take to preform
206 the repetitions of the operation being timed.
207 
208 \param test_size
209 will be used for the value of \c size in the call to \c test.
210 */
211 template <class Test>
212 double time_test(Test test, double time_min, size_t test_size)
213 {
214 # if CPPAD_EXTRA_RUN_BEFORE_TIMING
215  test(test_size, 1);
216 # endif
217  size_t repeat = 0;
218  double s0 = elapsed_seconds();
219  double s1 = s0;
220  while( s1 - s0 < time_min )
221  { repeat = std::max(size_t(1), 2 * repeat);
222  s0 = elapsed_seconds();
223  test(test_size, repeat);
224  s1 = elapsed_seconds();
225  }
226  double time = (s1 - s0) / double(repeat);
227  return time;
228 }
229 
230 } // END_CPPAD_NAMESPACE
231 
232 # undef CPPAD_EXTRA_RUN_BEFORE_TIMING
233 // END PROGRAM
234 # endif
double time_test(Test test, double time_min)
Preform one wall clock execution timing test.
Definition: time_test.hpp:172
Define processor symbols and macros that are used by CppAD.
Function that returns the elapsed seconds from first call.
double elapsed_seconds(void)
Returns the elapsed number since the first call to this function.