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