5798
|
1 ## Copyright (C) 2000-2006 Paul Kienzle |
5589
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301 USA |
|
17 |
|
18 ## -*- texinfo -*- |
5798
|
19 ## @deftypefn {Function File} {} speed (@var{f}, @var{init}, @var{max_n}, @var{f2}, @var{tol}) |
|
20 ## @deftypefnx {Function File} {[@var{order}, @var{n}, @var{T_f}, @var{T_f2}] =} speed (@dots{}) |
5589
|
21 ## |
|
22 ## Determine the execution time of an expression for various @var{n}. |
|
23 ## The @var{n} are log-spaced from 1 to @var{max_n}. For each @var{n}, |
|
24 ## an initialization expression is computed to create whatever data |
5798
|
25 ## are needed for the test. If a second expression is given, the |
|
26 ## execution times of the two expressions will be compared. Called |
|
27 ## without output arguments the results are presented graphically. |
5589
|
28 ## |
|
29 ## @table @code |
|
30 ## @item @var{f} |
|
31 ## The expression to evaluate. |
|
32 ## |
|
33 ## @item @var{max_n} |
5798
|
34 ## The maximum test length to run. Default value is 100. Alternatively, |
|
35 ## use @code{[min_n,max_n]} or for complete control, @code{[n1,n2,@dots{},nk]}. |
5589
|
36 ## |
|
37 ## @item @var{init} |
|
38 ## Initialization expression for function argument values. Use @var{k} |
|
39 ## for the test number and @var{n} for the size of the test. This should |
|
40 ## compute values for all variables listed in args. Note that init will |
|
41 ## be evaluated first for k=0, so things which are constant throughout |
|
42 ## the test can be computed then. The default value is @code{@var{x} = |
|
43 ## randn (@var{n}, 1);}. |
|
44 ## |
|
45 ## @item @var{f2} |
|
46 ## An alternative expression to evaluate, so the speed of the two |
|
47 ## can be compared. Default is @code{[]}. |
|
48 ## |
|
49 ## @item @var{tol} |
|
50 ## If @var{tol} is @code{Inf}, then no comparison will be made between the |
|
51 ## results of expression @var{f} and expression @var{f2}. Otherwise, |
|
52 ## expression @var{f} should produce a value @var{v} and expression @var{f2} |
|
53 ## should produce a value @var{v2}, and these shall be compared using |
6429
|
54 ## @code{assert(@var{v},@var{v2},@var{tol})}. If @var{tol} is positive, |
|
55 ## the tolerance is assumed to be absolutr. If @var{tol} is negative, |
|
56 ## the tolerance is assumed to be relative. The default is @code{eps}. |
5589
|
57 ## |
5798
|
58 ## @item @var{order} |
|
59 ## The time complexity of the expression @code{O(a n^p)}. This |
|
60 ## is a structure with fields @code{a} and @code{p}. |
5589
|
61 ## |
5798
|
62 ## @item @var{n} |
|
63 ## The values @var{n} for which the expression was calculated and the |
|
64 ## the execution time was greater than zero. |
5589
|
65 ## |
5798
|
66 ## @item @var{T_f} |
|
67 ## The nonzero execution times recorded for the expression @var{f} in seconds. |
|
68 ## |
|
69 ## @item @var{T_f2} |
|
70 ## The nonzero execution times recorded for the expression @var{f2} in seconds. |
|
71 ## If it is needed, the mean time ratio is just @code{mean(T_f./T_f2)}. |
|
72 ## |
5589
|
73 ## @end table |
|
74 ## |
5798
|
75 ## The slope of the execution time graph shows the approximate |
|
76 ## power of the asymptotic running time @code{O(n^p)}. This |
|
77 ## power is plotted for the region over which it is approximated |
|
78 ## (the latter half of the graph). The estimated power is not |
|
79 ## very accurate, but should be sufficient to determine the |
|
80 ## general order of your algorithm. It should indicate if for |
|
81 ## example your implementation is unexpectedly @code{O(n^2)} |
|
82 ## rather than @code{O(n)} because it extends a vector each |
|
83 ## time through the loop rather than preallocating one which is |
|
84 ## big enough. For example, in the current version of Octave, |
|
85 ## the following is not the expected @code{O(n)}: |
5589
|
86 ## |
5798
|
87 ## @example |
|
88 ## speed("for i=1:n,y@{i@}=x(i); end", "", [1000,10000]) |
|
89 ## @end example |
|
90 ## |
|
91 ## but it is if you preallocate the cell array @code{y}: |
5589
|
92 ## |
|
93 ## @example |
5798
|
94 ## speed("for i=1:n,y@{i@}=x(i);end", ... |
|
95 ## "x=rand(n,1);y=cell(size(x));", [1000,10000]) |
|
96 ## @end example |
|
97 ## |
|
98 ## An attempt is made to approximate the cost of the individual |
|
99 ## operations, but it is wildly inaccurate. You can improve the |
|
100 ## stability somewhat by doing more work for each @code{n}. For |
|
101 ## example: |
|
102 ## |
|
103 ## @example |
|
104 ## speed("airy(x)", "x=rand(n,10)", [10000,100000]) |
5589
|
105 ## @end example |
|
106 ## |
5798
|
107 ## When comparing a new and original expression, the line on the |
|
108 ## speedup ratio graph should be larger than 1 if the new expression |
|
109 ## is faster. Better algorithms have a shallow slope. Generally, |
|
110 ## vectorizing an algorithm will not change the slope of the execution |
|
111 ## time graph, but it will shift it relative to the original. For |
|
112 ## example: |
|
113 ## |
|
114 ## @example |
|
115 ## speed("v=sum(x)", "", [10000,100000], ... |
|
116 ## "v=0;for i=1:length(x),v+=x(i);end") |
|
117 ## @end example |
|
118 ## |
5589
|
119 ## A more complex example, if you had an original version of @code{xcorr} |
|
120 ## using for loops and another version using an FFT, you could compare the |
|
121 ## run speed for various lags as follows, or for a fixed lag with varying |
|
122 ## vector lengths as follows: |
|
123 ## |
|
124 ## @example |
|
125 ## speed("v=xcorr(x,n)", "x=rand(128,1);", 100, ... |
6429
|
126 ## "v2=xcorr_orig(x,n)", -100*eps) |
5589
|
127 ## speed("v=xcorr(x,15)", "x=rand(20+n,1);", 100, ... |
6429
|
128 ## "v2=xcorr_orig(x,n)", -100*eps) |
5589
|
129 ## @end example |
|
130 ## |
|
131 ## Assuming one of the two versions is in @var{xcorr_orig}, this would |
|
132 ## would compare their speed and their output values. Note that the |
|
133 ## FFT version is not exact, so we specify an acceptable tolerance on |
|
134 ## the comparison @code{100*eps}, and the errors should be computed |
|
135 ## relatively, as @code{abs((@var{x} - @var{y})./@var{y})} rather than |
|
136 ## absolutely as @code{abs(@var{x} - @var{y})}. |
|
137 ## |
|
138 ## Type @code{example('speed')} to see some real examples. Note for |
|
139 ## obscure reasons, you can't run examples 1 and 2 directly using |
|
140 ## @code{demo('speed')}. Instead use, @code{eval(example('speed',1))} |
|
141 ## and @code{eval(example('speed',2))}. |
|
142 ## @end deftypefn |
|
143 |
|
144 ## TODO: consider two dimensional speedup surfaces for functions like kron. |
5798
|
145 function [__order, __test_n, __tnew, __torig] ... |
|
146 = speed (__f1, __init, __max_n, __f2, __tol) |
5589
|
147 if nargin < 1 || nargin > 6, |
6046
|
148 print_usage (); |
5589
|
149 endif |
|
150 if nargin < 2 || isempty(__init), |
|
151 __init = "x = randn(n, 1);"; |
|
152 endif |
|
153 if nargin < 3 || isempty(__max_n), __max_n = 100; endif |
|
154 if nargin < 4, __f2 = []; endif |
|
155 if nargin < 5 || isempty(__tol), __tol = eps; endif |
5798
|
156 |
|
157 __numtests = 15; |
5589
|
158 |
5798
|
159 ## Let user specify range of n |
|
160 if isscalar(__max_n) |
|
161 __min_n = 1; |
|
162 assert(__max_n > __min_n); |
|
163 __test_n = logspace(0,log10(__max_n),__numtests); |
|
164 elseif length(__max_n) == 2 |
|
165 __min_n = __max_n(1); |
|
166 __max_n = __max_n(2); |
|
167 assert(__min_n >= 1); |
|
168 __test_n = logspace(log10(__min_n),log10(__max_n),__numtests); |
|
169 else |
|
170 __test_n = __max_n; |
|
171 endif |
|
172 __test_n = unique(round(__test_n)); # Force n to be an integer |
|
173 assert(__test_n >= 1); |
5589
|
174 |
|
175 __torig = __tnew = zeros (size(__test_n)) ; |
|
176 |
5798
|
177 disp (["testing ", __f1, "\ninit: ", __init]); |
5589
|
178 |
|
179 ## make sure the functions are freshly loaded by evaluating them at |
5798
|
180 ## test_n(1); first have to initialize the args though. |
5589
|
181 n=1; k=0; |
|
182 eval ([__init, ";"]); |
|
183 if !isempty(__f2), eval ([__f2, ";"]); endif |
|
184 eval ([__f1, ";"]); |
|
185 |
|
186 ## run the tests |
|
187 for k=1:length(__test_n) |
5798
|
188 n=__test_n(k); |
|
189 eval ([__init, ";"]); |
5589
|
190 |
|
191 printf ("n%i=%i ",k, n) ; fflush(1); |
|
192 eval (["__t=time();", __f1, "; __v1=ans; __t = time()-__t;"]); |
|
193 if (__t < 0.25) |
|
194 eval (["__t2=time();", __f1, "; __t2 = time()-__t2;"]); |
|
195 eval (["__t3=time();", __f1, "; __t3 = time()-__t3;"]); |
|
196 __t = min([__t,__t2,__t3]); |
|
197 endif |
|
198 __tnew(k) = __t; |
|
199 |
|
200 if !isempty(__f2) |
|
201 eval (["__t=time();", __f2, "; __v2=ans; __t = time()-__t;"]); |
|
202 if (__t < 0.25) |
|
203 eval (["__t2=time();", __f2, "; __t2 = time()-__t2;"]); |
|
204 eval (["__t3=time();", __f2, "; __t3 = time()-__t3;"]); |
|
205 endif |
|
206 __torig(k) = __t; |
|
207 if !isinf(__tol) |
5798
|
208 assert(__v1,__v2,__tol); |
5589
|
209 endif |
|
210 endif |
|
211 |
5798
|
212 endfor |
5589
|
213 |
5798
|
214 ## Drop times of zero |
|
215 if !isempty(__f2) |
|
216 zidx = ( __tnew < 100*eps | __torig < 100*eps ) ; |
|
217 __test_n(zidx) = []; |
|
218 __tnew(zidx) = []; |
|
219 __torig(zidx) = []; |
5589
|
220 else |
5798
|
221 zidx = ( __tnew < 100*eps ) ; |
|
222 __test_n(zidx) = []; |
|
223 __tnew(zidx) = []; |
5589
|
224 endif |
5798
|
225 |
|
226 ## Approximate time complexity and return it if requested |
|
227 tailidx = [ceil(length(__test_n)/2):length(__test_n)]; |
|
228 p = polyfit(log(__test_n(tailidx)),log(__tnew(tailidx)), 1); |
|
229 if nargout > 0, |
|
230 __order.p = p(1); |
|
231 __order.a = exp(p(2)); |
|
232 endif |
|
233 |
5589
|
234 |
5798
|
235 ## Plot the data if no output is requested. |
|
236 doplot = (nargout == 0); |
6430
|
237 |
|
238 if (doplot) |
|
239 figure; |
|
240 endif |
5798
|
241 |
|
242 if doplot && !isempty(__f2) |
5589
|
243 subplot(121); |
6430
|
244 hold on; |
5589
|
245 xlabel("test length"); |
|
246 title (__f1); |
|
247 ylabel("speedup ratio"); |
5798
|
248 semilogx ( __test_n, __torig./__tnew, |
5589
|
249 ["-*r;", strrep(__f1,";","."), "/", strrep(__f2,";","."), ";"], |
5798
|
250 __test_n, __tnew./__torig, |
5589
|
251 ["-*g;", strrep(__f2,";","."), "/", strrep(__f1,";","."), ";"]); |
|
252 subplot (122); |
6430
|
253 hold on; |
|
254 xlabel("test length"); |
5589
|
255 ylabel ("best execution time (ms)"); |
|
256 title (["init: ", __init]); |
5798
|
257 loglog ( __test_n, __tnew*1000, ["*-g;", strrep(__f1,";","."), ";" ], |
|
258 __test_n, __torig*1000, ["*-r;", strrep(__f2,";","."), ";"]) |
|
259 |
|
260 ratio = mean (__torig ./ __tnew); |
|
261 printf ("\n\nMean runtime ratio = %.3g for '%s' vs '%s'\n", ... |
|
262 ratio, __f2, __f1); |
|
263 |
|
264 elseif doplot |
|
265 |
|
266 subplot(111); |
6430
|
267 hold on; |
5589
|
268 xlabel("test length"); |
|
269 ylabel ("best execution time (ms)"); |
|
270 title ([__f1, " init: ", __init]); |
5798
|
271 loglog ( __test_n, __tnew*1000, "*-g;execution time;"); |
|
272 |
5589
|
273 endif |
5798
|
274 |
|
275 if doplot |
|
276 |
|
277 ## Plot time complexity approximation (using milliseconds). |
|
278 order = sprintf("O(n^%g)",round(10*p(1))/10); |
|
279 v = polyval(p,log(__test_n(tailidx))); |
|
280 hold on; |
|
281 loglog(__test_n(tailidx), exp(v)*1000, sprintf("b;%s;",order)); |
|
282 hold off; |
|
283 |
|
284 ## Get base time to 1 digit of accuracy |
|
285 dt = exp(p(2)); |
|
286 dt = floor(dt/10^floor(log10(dt)))*10^floor(log10(dt)); |
|
287 if log10(dt) >= -0.5, time = sprintf("%g s", dt); |
|
288 elseif log10(dt) >= -3.5, time = sprintf("%g ms", dt*1e3); |
|
289 elseif log10(dt) >= -6.5, time = sprintf("%g us", dt*1e6); |
|
290 else time = sprintf("%g ns", dt*1e9); |
|
291 endif |
|
292 |
|
293 ## Display nicely formatted complexity. |
|
294 printf ("\nFor %s:\n",__f1); |
|
295 printf (" asymptotic power: %s\n", order); |
|
296 printf (" approximate time per operation: %s\n", time); |
|
297 |
|
298 endif |
|
299 |
5589
|
300 endfunction |
|
301 |
|
302 %!demo if 1 |
|
303 %! function x = build_orig(n) |
|
304 %! ## extend the target vector on the fly |
|
305 %! for i=0:n-1, x([1:10]+i*10) = 1:10; endfor |
|
306 %! endfunction |
|
307 %! function x = build(n) |
|
308 %! ## preallocate the target vector |
|
309 %! x = zeros(1, n*10); |
|
310 %! try |
|
311 %! if (prefer_column_vectors), x = x.'; endif |
|
312 %! catch |
|
313 %! end |
|
314 %! for i=0:n-1, x([1:10]+i*10) = 1:10; endfor |
|
315 %! endfunction |
|
316 %! |
|
317 %! disp("-----------------------"); |
|
318 %! type build_orig; |
|
319 %! disp("-----------------------"); |
|
320 %! type build; |
|
321 %! disp("-----------------------"); |
|
322 %! |
|
323 %! disp("Preallocated vector test.\nThis takes a little while..."); |
6429
|
324 %! speed('build(n)', '', 1000, 'build_orig(n)'); |
5589
|
325 %! clear build build_orig |
|
326 %! disp("Note how much faster it is to pre-allocate a vector."); |
|
327 %! disp("Notice the peak speedup ratio."); |
|
328 %! endif |
|
329 |
|
330 %!demo if 1 |
|
331 %! function x = build_orig(n) |
|
332 %! for i=0:n-1, x([1:10]+i*10) = 1:10; endfor |
|
333 %! endfunction |
|
334 %! function x = build(n) |
|
335 %! idx = [1:10]'; |
|
336 %! x = idx(:,ones(1,n)); |
|
337 %! x = reshape(x, 1, n*10); |
|
338 %! try |
|
339 %! if (prefer_column_vectors), x = x.'; endif |
|
340 %! catch |
|
341 %! end |
|
342 %! endfunction |
|
343 %! |
|
344 %! disp("-----------------------"); |
|
345 %! type build_orig; |
|
346 %! disp("-----------------------"); |
|
347 %! type build; |
|
348 %! disp("-----------------------"); |
|
349 %! |
|
350 %! disp("Vectorized test. This takes a little while..."); |
6429
|
351 %! speed('build(n)', '', 1000, 'build_orig(n)'); |
5589
|
352 %! clear build build_orig |
|
353 %! disp("-----------------------"); |
|
354 %! disp("This time, the for loop is done away with entirely."); |
|
355 %! disp("Notice how much bigger the speedup is then in example 1."); |
|
356 %! endif |