Mercurial > octave-antonio
annotate scripts/testfun/speed.m @ 11314:87f258202b0f
speed.m: Overhaul documentation string.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 06 Dec 2010 17:03:00 -0800 |
parents | a4f482e66b65 |
children | cc7f30d3fd01 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, |
2 ## 2009 Paul Kienzle | |
7016 | 3 ## |
4 ## This file is part of Octave. | |
5589 | 5 ## |
7016 | 6 ## Octave is free software; you can redistribute it and/or modify it |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
5589 | 10 ## |
7016 | 11 ## Octave 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. | |
5589 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5589 | 19 |
20 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
21 ## @deftypefn {Function File} {} speed (@var{f}, @var{init}, @var{max_n}, @var{f2}, @var{tol}) |
5798 | 22 ## @deftypefnx {Function File} {[@var{order}, @var{n}, @var{T_f}, @var{T_f2}] =} speed (@dots{}) |
5589 | 23 ## |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
24 ## Determine the execution time of an expression (@var{f}) for various input |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
25 ## values (@var{n}). The @var{n} are log-spaced from 1 to @var{max_n}. For |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
26 ## each @var{n}, an initialization expression (@var{init}) is computed to |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
27 ## create any data needed for the test. If a second expression (@var{f2}) is |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
28 ## given then the execution times of the two expressions are compared. When |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
29 ## called without output arguments the results are displayed graphically. |
5589 | 30 ## |
31 ## @table @code | |
32 ## @item @var{f} | |
33 ## The expression to evaluate. | |
34 ## | |
35 ## @item @var{max_n} | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
36 ## The maximum test length to run. Default value is 100. Alternatively, |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
37 ## use @code{[min_n, max_n]} or specify the @var{n} exactly with |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
38 ## @code{[n1, n2, @dots{}, nk]}. |
5589 | 39 ## |
40 ## @item @var{init} | |
41 ## Initialization expression for function argument values. Use @var{k} | |
42 ## for the test number and @var{n} for the size of the test. This should | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
43 ## compute values for all variables used by @var{f}. Note that @var{init} will |
8507 | 44 ## be evaluated first for @math{k = 0}, so things which are constant throughout |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
45 ## the test series can be computed once. The default value is |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
46 ## @code{@var{x} = randn (@var{n}, 1)}. |
5589 | 47 ## |
48 ## @item @var{f2} | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
49 ## An alternative expression to evaluate, so that the speed of two |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
50 ## expressions can be directly compared. The default is @code{[]}. |
5589 | 51 ## |
52 ## @item @var{tol} | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
53 ## Tolerance used to compare the results of expression @var{f} and expression |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
54 ## @var{f2}. If @var{tol} is positive, the tolerance is an absolute one. |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
55 ## If @var{tol} is negative, the tolerance is a relative one. The default is |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
56 ## @code{eps}. If @var{tol} is @code{Inf}, then no comparison will be made. |
5589 | 57 ## |
5798 | 58 ## @item @var{order} |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
59 ## The time complexity of the expression @math{O(a*n^p)}. This |
5798 | 60 ## is a structure with fields @code{a} and @code{p}. |
5589 | 61 ## |
5798 | 62 ## @item @var{n} |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
63 ## The values @var{n} for which the expression was calculated AND |
5798 | 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. | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
71 ## If required, the mean time ratio is simply @code{mean (T_f./T_f2)}. |
5798 | 72 ## |
5589 | 73 ## @end table |
74 ## | |
5798 | 75 ## The slope of the execution time graph shows the approximate |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
76 ## power of the asymptotic running time @math{O(n^p)}. This |
5798 | 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 | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
80 ## general order of an algorithm. It should indicate if, for |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
81 ## example, the implementation is unexpectedly @math{O(n^2)} |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
82 ## rather than @math{O(n)} because it extends a vector each |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
83 ## time through the loop rather than pre-allocating storage. |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
84 ## In the current version of Octave, the following is not the |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
85 ## expected @math{O(n)}. |
5589 | 86 ## |
5798 | 87 ## @example |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
88 ## speed ("for i = 1:n, y@{i@} = x(i); endfor", "", [1000, 10000]) |
5798 | 89 ## @end example |
90 ## | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
91 ## @noindent |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
92 ## But it is if you preallocate the cell array @code{y}: |
5589 | 93 ## |
94 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
95 ## @group |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
96 ## speed ("for i = 1:n, y@{i@} = x(i); endfor", ... |
8507 | 97 ## "x = rand (n, 1); y = cell (size (x));", [1000, 10000]) |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
98 ## @end group |
5798 | 99 ## @end example |
100 ## | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
101 ## An attempt is made to approximate the cost of individual |
5798 | 102 ## operations, but it is wildly inaccurate. You can improve the |
103 ## stability somewhat by doing more work for each @code{n}. For | |
104 ## example: | |
105 ## | |
106 ## @example | |
8507 | 107 ## speed ("airy(x)", "x = rand (n, 10)", [10000, 100000]) |
5589 | 108 ## @end example |
109 ## | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
110 ## When comparing two different expressions (@var{f}, @var{f2}), the slope |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
111 ## of the line on the speedup ratio graph should be larger than 1 if the new |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
112 ## expression is faster. Better algorithms have a shallow slope. Generally, |
5798 | 113 ## vectorizing an algorithm will not change the slope of the execution |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
114 ## time graph, but will shift it relative to the original. For |
5798 | 115 ## example: |
116 ## | |
117 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
118 ## @group |
8507 | 119 ## speed ("v = sum (x)", "", [10000, 100000], ... |
120 ## "v = 0; for i = 1:length (x), v += x(i); end") | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
121 ## @end group |
5798 | 122 ## @end example |
123 ## | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
124 ## The following is a more complex example. If there was an original version |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
125 ## of @code{xcorr} using for loops and a second version using an FFT, then |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
126 ## one could compare the run speed for various lags as follows, or for a fixed |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
127 ## lag with varying vector lengths as follows: |
5589 | 128 ## |
129 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
130 ## @group |
8507 | 131 ## speed ("v = xcorr (x, n)", "x = rand (128, 1);", 100, |
132 ## "v2 = xcorr_orig (x, n)", -100*eps) | |
133 ## speed ("v = xcorr (x, 15)", "x = rand (20+n, 1);", 100, | |
134 ## "v2 = xcorr_orig (x, n)", -100*eps) | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
135 ## @end group |
5589 | 136 ## @end example |
137 ## | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
138 ## Assuming one of the two versions is in xcorr_orig, this |
5589 | 139 ## would compare their speed and their output values. Note that the |
140 ## FFT version is not exact, so we specify an acceptable tolerance on | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
141 ## the comparison @code{100*eps}, and that the errors should be computed |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
142 ## relatively, as @code{abs ((@var{x} - @var{y}) ./ @var{y})} rather than |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
143 ## absolutely as @code{abs (@var{x} - @var{y})}. |
5589 | 144 ## |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
145 ## Type @code{example('speed')} to see some real examples. Note that for |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
146 ## obscure reasons, examples 1 and 2 can not be run directly using |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
147 ## @code{demo('speed')}. Instead use, @code{eval ( example('speed', 1) )} |
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
148 ## or @code{eval ( example('speed', 2) )}. |
5589 | 149 ## @end deftypefn |
150 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7540
diff
changeset
|
151 ## FIXME: consider two dimensional speedup surfaces for functions like kron. |
5798 | 152 function [__order, __test_n, __tnew, __torig] ... |
6494 | 153 = speed (__f1, __init, __max_n, __f2, __tol) |
154 | |
155 if (nargin < 1 || nargin > 6) | |
6046 | 156 print_usage (); |
5589 | 157 endif |
6494 | 158 |
159 if (nargin < 2 || isempty (__init)) | |
5589 | 160 __init = "x = randn(n, 1);"; |
161 endif | |
6494 | 162 |
163 if (nargin < 3 || isempty (__max_n)) | |
164 __max_n = 100; | |
165 endif | |
166 | |
167 if (nargin < 4) | |
168 __f2 = []; | |
169 endif | |
170 | |
171 if (nargin < 5 || isempty (__tol)) | |
172 __tol = eps; | |
173 endif | |
5798 | 174 |
175 __numtests = 15; | |
5589 | 176 |
8506 | 177 ## Let user specify range of n. |
6494 | 178 if (isscalar (__max_n)) |
5798 | 179 __min_n = 1; |
6494 | 180 assert (__max_n > __min_n); |
181 __test_n = logspace (0, log10 (__max_n), __numtests); | |
182 elseif (length (__max_n) == 2) | |
5798 | 183 __min_n = __max_n(1); |
184 __max_n = __max_n(2); | |
6494 | 185 assert (__min_n >= 1); |
186 __test_n = logspace (log10 (__min_n), log10 (__max_n), __numtests); | |
5798 | 187 else |
188 __test_n = __max_n; | |
189 endif | |
8506 | 190 ## Force n to be an integer. |
191 __test_n = unique (round (__test_n)); | |
6494 | 192 assert (__test_n >= 1); |
5589 | 193 |
6494 | 194 __torig = __tnew = zeros (size (__test_n)); |
5589 | 195 |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
196 disp (cstrcat ("testing ", __f1, "\ninit: ", __init)); |
5589 | 197 |
8506 | 198 ## Make sure the functions are freshly loaded by evaluating them at |
5798 | 199 ## test_n(1); first have to initialize the args though. |
6494 | 200 n = 1; |
201 k = 0; | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
202 eval (cstrcat (__init, ";")); |
6494 | 203 if (! isempty (__f2)) |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
204 eval (cstrcat (__f2, ";")); |
6494 | 205 endif |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
206 eval (cstrcat (__f1, ";")); |
5589 | 207 |
8506 | 208 ## Run the tests. |
6494 | 209 for k = 1:length (__test_n) |
210 n = __test_n(k); | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
211 eval (cstrcat (__init, ";")); |
5589 | 212 |
8507 | 213 printf ("n%i = %i ",k, n); |
6494 | 214 fflush (stdout); |
8507 | 215 eval (cstrcat ("__t = time();", __f1, "; __v1=ans; __t = time()-__t;")); |
5589 | 216 if (__t < 0.25) |
8507 | 217 eval (cstrcat ("__t2 = time();", __f1, "; __t2 = time()-__t2;")); |
218 eval (cstrcat ("__t3 = time();", __f1, "; __t3 = time()-__t3;")); | |
6494 | 219 __t = min ([__t, __t2, __t3]); |
5589 | 220 endif |
221 __tnew(k) = __t; | |
222 | |
6494 | 223 if (! isempty (__f2)) |
8507 | 224 eval (cstrcat ("__t = time();", __f2, "; __v2=ans; __t = time()-__t;")); |
5589 | 225 if (__t < 0.25) |
10549 | 226 eval (cstrcat ("__t2 = time();", __f2, "; __t2 = time()-__t2;")); |
227 eval (cstrcat ("__t3 = time();", __f2, "; __t3 = time()-__t3;")); | |
5589 | 228 endif |
229 __torig(k) = __t; | |
6494 | 230 if (! isinf(__tol)) |
10549 | 231 assert (__v1, __v2, __tol); |
5589 | 232 endif |
233 endif | |
5798 | 234 endfor |
5589 | 235 |
8506 | 236 ## Drop times of zero. |
6494 | 237 if (! isempty (__f2)) |
238 zidx = (__tnew < 100*eps | __torig < 100*eps); | |
5798 | 239 __test_n(zidx) = []; |
240 __tnew(zidx) = []; | |
241 __torig(zidx) = []; | |
5589 | 242 else |
6494 | 243 zidx = (__tnew < 100*eps); |
5798 | 244 __test_n(zidx) = []; |
245 __tnew(zidx) = []; | |
5589 | 246 endif |
6494 | 247 |
8506 | 248 ## Approximate time complexity and return it if requested. |
6494 | 249 tailidx = ceil(length(__test_n)/2):length(__test_n); |
250 p = polyfit (log (__test_n(tailidx)), log (__tnew(tailidx)), 1); | |
251 if (nargout > 0) | |
5798 | 252 __order.p = p(1); |
6494 | 253 __order.a = exp (p(2)); |
5798 | 254 endif |
5589 | 255 |
5798 | 256 ## Plot the data if no output is requested. |
257 doplot = (nargout == 0); | |
6430 | 258 |
259 if (doplot) | |
260 figure; | |
261 endif | |
5798 | 262 |
6494 | 263 if (doplot && ! isempty (__f2)) |
264 subplot (1, 2, 1); | |
265 semilogx (__test_n, __torig./__tnew, | |
10549 | 266 cstrcat ("-*r;", strrep (__f1, ";", "."), "/", |
267 strrep (__f2, ";", "."), ";"), | |
268 __test_n, __tnew./__torig, | |
269 cstrcat ("-*g;", strrep (__f2, ";", "."), "/", | |
270 strrep (__f1, ";", "."), ";")); | |
6494 | 271 xlabel ("test length"); |
272 title (__f1); | |
273 ylabel ("speedup ratio"); | |
274 | |
275 subplot (1, 2, 2); | |
276 loglog (__test_n, __tnew*1000, | |
10549 | 277 cstrcat ("*-g;", strrep (__f1, ";", "."), ";"), |
278 __test_n, __torig*1000, | |
279 cstrcat ("*-r;", strrep (__f2,";","."), ";")); | |
6494 | 280 |
281 xlabel ("test length"); | |
5589 | 282 ylabel ("best execution time (ms)"); |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
283 title (cstrcat ("init: ", __init)); |
6494 | 284 |
5798 | 285 ratio = mean (__torig ./ __tnew); |
6494 | 286 printf ("\n\nMean runtime ratio = %.3g for '%s' vs '%s'\n", |
5798 | 287 ratio, __f2, __f1); |
288 | |
6494 | 289 elseif (doplot) |
5798 | 290 |
6494 | 291 loglog (__test_n, __tnew*1000, "*-g;execution time;"); |
292 xlabel ("test length"); | |
5589 | 293 ylabel ("best execution time (ms)"); |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7017
diff
changeset
|
294 title (cstrcat (__f1, " init: ", __init)); |
5798 | 295 |
5589 | 296 endif |
5798 | 297 |
6494 | 298 if (doplot) |
5798 | 299 |
300 ## Plot time complexity approximation (using milliseconds). | |
6494 | 301 order = sprintf ("O(n^%g)", round (10*p(1))/10); |
302 v = polyval (p, log (__test_n(tailidx))); | |
303 | |
304 loglog (__test_n(tailidx), exp(v)*1000, sprintf ("b;%s;", order)); | |
5798 | 305 |
8506 | 306 ## Get base time to 1 digit of accuracy. |
6494 | 307 dt = exp (p(2)); |
308 dt = floor (dt/10^floor(log10(dt)))*10^floor(log10(dt)); | |
309 if (log10 (dt) >= -0.5) | |
310 time = sprintf ("%g s", dt); | |
311 elseif (log10 (dt) >= -3.5) | |
312 time = sprintf ("%g ms", dt*1e3); | |
313 elseif (log10 (dt) >= -6.5) | |
314 time = sprintf ("%g us", dt*1e6); | |
315 else | |
316 time = sprintf ("%g ns", dt*1e9); | |
5798 | 317 endif |
318 | |
319 ## Display nicely formatted complexity. | |
6494 | 320 printf ("\nFor %s:\n", __f1); |
5798 | 321 printf (" asymptotic power: %s\n", order); |
322 printf (" approximate time per operation: %s\n", time); | |
323 | |
324 endif | |
325 | |
5589 | 326 endfunction |
327 | |
328 %!demo if 1 | |
329 %! function x = build_orig(n) | |
330 %! ## extend the target vector on the fly | |
331 %! for i=0:n-1, x([1:10]+i*10) = 1:10; endfor | |
332 %! endfunction | |
333 %! function x = build(n) | |
334 %! ## preallocate the target vector | |
335 %! x = zeros(1, n*10); | |
336 %! try | |
337 %! if (prefer_column_vectors), x = x.'; endif | |
338 %! catch | |
339 %! end | |
340 %! for i=0:n-1, x([1:10]+i*10) = 1:10; endfor | |
341 %! endfunction | |
342 %! | |
343 %! disp("-----------------------"); | |
344 %! type build_orig; | |
345 %! disp("-----------------------"); | |
346 %! type build; | |
347 %! disp("-----------------------"); | |
348 %! | |
349 %! disp("Preallocated vector test.\nThis takes a little while..."); | |
6429 | 350 %! speed('build(n)', '', 1000, 'build_orig(n)'); |
5589 | 351 %! clear build build_orig |
352 %! disp("Note how much faster it is to pre-allocate a vector."); | |
353 %! disp("Notice the peak speedup ratio."); | |
354 %! endif | |
355 | |
356 %!demo if 1 | |
357 %! function x = build_orig(n) | |
358 %! for i=0:n-1, x([1:10]+i*10) = 1:10; endfor | |
359 %! endfunction | |
360 %! function x = build(n) | |
361 %! idx = [1:10]'; | |
362 %! x = idx(:,ones(1,n)); | |
363 %! x = reshape(x, 1, n*10); | |
364 %! try | |
365 %! if (prefer_column_vectors), x = x.'; endif | |
366 %! catch | |
367 %! end | |
368 %! endfunction | |
369 %! | |
370 %! disp("-----------------------"); | |
371 %! type build_orig; | |
372 %! disp("-----------------------"); | |
373 %! type build; | |
374 %! disp("-----------------------"); | |
375 %! | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
376 %! disp("Vectorized test.\nThis takes a little while..."); |
6429 | 377 %! speed('build(n)', '', 1000, 'build_orig(n)'); |
5589 | 378 %! clear build build_orig |
379 %! disp("-----------------------"); | |
380 %! disp("This time, the for loop is done away with entirely."); | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
381 %! disp("Notice how much bigger the speedup is than in example 1."); |
5589 | 382 %! endif |