Mercurial > hg > octave-lyh
annotate scripts/testfun/speed.m @ 16488:d4ce14c19c2c
Fix print to work with filenames which begin with '~' (bug #38694).
* scripts/plot/private/__print_parse_opts__.m: Call tilde_expand()
on filename input.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 10 Apr 2013 16:41:06 -0700 |
parents | 5d3a684236b0 |
children | 333243133364 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13283
diff
changeset
|
1 ## Copyright (C) 2000-2012 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 |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
28 ## called without output arguments the results are printed to stdout and |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
29 ## displayed graphically. |
5589 | 30 ## |
31 ## @table @code | |
32 ## @item @var{f} | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
33 ## The code expression to evaluate. |
5589 | 34 ## |
35 ## @item @var{max_n} | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
36 ## The maximum test length to run. The 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
|
37 ## 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
|
38 ## @code{[n1, n2, @dots{}, nk]}. |
5589 | 39 ## |
40 ## @item @var{init} | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
41 ## Initialization expression for function argument values. Use @var{k} |
5589 | 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 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
45 ## 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
|
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} |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
63 ## The values @var{n} for which the expression was calculated @strong{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. | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
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 | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
80 ## 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
|
81 ## 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
|
82 ## 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
|
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 ## | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
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 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
112 ## 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
|
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 |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
119 ## speed ("sum (x)", "", [10000, 100000], ... |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
120 ## "v = 0; for i = 1:length (x), v += x(i); endfor") |
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 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
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 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
125 ## 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
|
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 |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
131 ## speed ("xcorr (x, n)", "x = rand (128, 1);", 100, |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
132 ## "xcorr_orig (x, n)", -100*eps) |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
133 ## speed ("xcorr (x, 15)", "x = rand (20+n, 1);", 100, |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
134 ## "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 |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
140 ## FFT version is not exact, so one must specify an acceptable tolerance on |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
141 ## the comparison @code{100*eps}. In this case, the comparison should be |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
142 ## computed relatively, as @code{abs ((@var{x} - @var{y}) ./ @var{y})} rather |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
143 ## than absolutely as @code{abs (@var{x} - @var{y})}. |
5589 | 144 ## |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
145 ## Type @kbd{example ("speed")} to see some real examples or |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
146 ## @kbd{demo ("speed")} to run them. |
5589 | 147 ## @end deftypefn |
148 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7540
diff
changeset
|
149 ## FIXME: consider two dimensional speedup surfaces for functions like kron. |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
150 function [__order, __test_n, __tnew, __torig] = speed (__f1, __init, __max_n = 100, __f2 = "", __tol = eps) |
6494 | 151 |
152 if (nargin < 1 || nargin > 6) | |
6046 | 153 print_usage (); |
5589 | 154 endif |
6494 | 155 |
156 if (nargin < 2 || isempty (__init)) | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
157 __init = "x = randn (n, 1)"; |
6494 | 158 endif |
159 | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
160 if (isempty (__max_n)) |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
161 __max_n = 100; |
6494 | 162 endif |
5798 | 163 |
164 __numtests = 15; | |
5589 | 165 |
8506 | 166 ## Let user specify range of n. |
6494 | 167 if (isscalar (__max_n)) |
5798 | 168 __min_n = 1; |
6494 | 169 assert (__max_n > __min_n); |
170 __test_n = logspace (0, log10 (__max_n), __numtests); | |
171 elseif (length (__max_n) == 2) | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
172 [__min_n, __max_n] = deal (__max_n(1), __max_n(2)); |
6494 | 173 assert (__min_n >= 1); |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
174 assert (__max_n > __min_n); |
6494 | 175 __test_n = logspace (log10 (__min_n), log10 (__max_n), __numtests); |
5798 | 176 else |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
177 assert (all (__max_n > 0)); |
5798 | 178 __test_n = __max_n; |
179 endif | |
8506 | 180 ## Force n to be an integer. |
181 __test_n = unique (round (__test_n)); | |
6494 | 182 assert (__test_n >= 1); |
5589 | 183 |
6494 | 184 __torig = __tnew = zeros (size (__test_n)); |
5589 | 185 |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
186 ## Print and plot the data if no output is requested. |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
187 do_display = (nargout == 0); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
188 |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
189 if (do_display) |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
190 disp (cstrcat ("testing ", __f1, "\ninit: ", __init)); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
191 endif |
5589 | 192 |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
193 ## Add semicolon closure to all code fragments in case user has not done so. |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
194 __init = cstrcat (__init, ";"); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
195 __f1 = cstrcat (__f1, ";"); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
196 if (! isempty (__f2)) |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
197 __f2 = cstrcat (__f2, ";"); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
198 endif |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
199 |
8506 | 200 ## Make sure the functions are freshly loaded by evaluating them at |
5798 | 201 ## test_n(1); first have to initialize the args though. |
6494 | 202 n = 1; |
203 k = 0; | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
204 eval (__init); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
205 eval (__f1); |
6494 | 206 if (! isempty (__f2)) |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
207 eval (__f2); |
6494 | 208 endif |
5589 | 209 |
8506 | 210 ## Run the tests. |
6494 | 211 for k = 1:length (__test_n) |
212 n = __test_n(k); | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
213 eval (__init); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
214 |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
215 if (do_display) |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
216 printf ("n%i = %i ", k, n); |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
217 fflush (stdout); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
218 endif |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
219 |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
220 eval (cstrcat ("__t = time();", __f1, "__v1=ans; __t = time()-__t;")); |
5589 | 221 if (__t < 0.25) |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
222 eval (cstrcat ("__t2 = time();", __f1, "__t2 = time()-__t2;")); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
223 eval (cstrcat ("__t3 = time();", __f1, "__t3 = time()-__t3;")); |
6494 | 224 __t = min ([__t, __t2, __t3]); |
5589 | 225 endif |
226 __tnew(k) = __t; | |
227 | |
6494 | 228 if (! isempty (__f2)) |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
229 eval (cstrcat ("__t = time();", __f2, "__v2=ans; __t = time()-__t;")); |
5589 | 230 if (__t < 0.25) |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
231 eval (cstrcat ("__t2 = time();", __f2, "__t2 = time()-__t2;")); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
232 eval (cstrcat ("__t3 = time();", __f2, "__t3 = time()-__t3;")); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
233 __t = min ([__t, __t2, __t3]); |
5589 | 234 endif |
235 __torig(k) = __t; | |
6494 | 236 if (! isinf(__tol)) |
10549 | 237 assert (__v1, __v2, __tol); |
5589 | 238 endif |
239 endif | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
240 |
5798 | 241 endfor |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
242 |
8506 | 243 ## Drop times of zero. |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
244 if (isempty (__f2)) |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
245 zidx = (__tnew < 100*eps); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
246 __test_n(zidx) = []; |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
247 __tnew(zidx) = []; |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
248 else |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
249 zidx = (__tnew < 100*eps | __torig < 100*eps); |
5798 | 250 __test_n(zidx) = []; |
251 __tnew(zidx) = []; | |
252 __torig(zidx) = []; | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
253 endif |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
254 |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
255 if (isempty (__test_n)) |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
256 error (["speed: All running times were zero.\n", |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
257 "error: speed: Choose larger MAX_N or do more work per function evaluation"]); |
5589 | 258 endif |
6494 | 259 |
8506 | 260 ## Approximate time complexity and return it if requested. |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
261 tailidx = ceil (length (__test_n)/2):length (__test_n); |
6494 | 262 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
|
263 if (nargout > 0) |
5798 | 264 __order.p = p(1); |
6494 | 265 __order.a = exp (p(2)); |
5798 | 266 endif |
5589 | 267 |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
268 if (do_display) |
6430 | 269 figure; |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
270 ## Strip semicolon added to code fragments before displaying |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
271 __init(end) = ""; |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
272 __f1(end) = ""; |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
273 if (! isempty (__f2)) |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
274 __f2(end) = ""; |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
275 endif |
6430 | 276 endif |
5798 | 277 |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
278 if (do_display && isempty (__f2)) |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
279 |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
280 loglog (__test_n, __tnew*1000, "*-g;execution time;"); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
281 xlabel ("test length"); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
282 ylabel ("best execution time (ms)"); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
283 title ({__f1, cstrcat("init: ", __init)}); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
284 |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
285 elseif (do_display) |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
286 |
6494 | 287 subplot (1, 2, 1); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
288 semilogx (__test_n, __torig./__tnew, |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
289 cstrcat ("-*r;", strrep (__f1, ";", "."), " / ", |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
290 strrep (__f2, ";", "."), ";"), |
10549 | 291 __test_n, __tnew./__torig, |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
292 cstrcat ("-*g;", strrep (__f2, ";", "."), " / ", |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
293 strrep (__f1, ";", "."), ";")); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
294 title ("Speedup Ratio"); |
6494 | 295 xlabel ("test length"); |
296 ylabel ("speedup ratio"); | |
297 | |
298 subplot (1, 2, 2); | |
299 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
|
300 cstrcat ("*-g;", strrep (__f1, ";", "."), ";"), |
10549 | 301 __test_n, __torig*1000, |
302 cstrcat ("*-r;", strrep (__f2,";","."), ";")); | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
303 title ({"Execution Times", cstrcat("init: ", __init)}); |
6494 | 304 xlabel ("test length"); |
5589 | 305 ylabel ("best execution time (ms)"); |
6494 | 306 |
5798 | 307 ratio = mean (__torig ./ __tnew); |
6494 | 308 printf ("\n\nMean runtime ratio = %.3g for '%s' vs '%s'\n", |
5798 | 309 ratio, __f2, __f1); |
310 | |
5589 | 311 endif |
5798 | 312 |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
313 if (do_display) |
5798 | 314 |
315 ## Plot time complexity approximation (using milliseconds). | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
316 figure; # Open second plot window |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
317 |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
318 order = round (10*p(1))/10; |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
319 if (order >= 0.1) |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
320 order = sprintf ("O(n^%g)", order); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
321 else |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
322 order = "O(1)"; |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
323 endif |
6494 | 324 v = polyval (p, log (__test_n(tailidx))); |
325 | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
326 loglog (__test_n(tailidx), exp (v) * 1000, sprintf ("b;%s;", order)); |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
327 title ({"Time Complexity", __f1}); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
328 xlabel ("test length"); |
5798 | 329 |
8506 | 330 ## Get base time to 1 digit of accuracy. |
6494 | 331 dt = exp (p(2)); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
332 dt = floor (dt/10^floor (log10 (dt)))*10^floor (log10 (dt)); |
6494 | 333 if (log10 (dt) >= -0.5) |
334 time = sprintf ("%g s", dt); | |
335 elseif (log10 (dt) >= -3.5) | |
336 time = sprintf ("%g ms", dt*1e3); | |
337 elseif (log10 (dt) >= -6.5) | |
338 time = sprintf ("%g us", dt*1e6); | |
339 else | |
340 time = sprintf ("%g ns", dt*1e9); | |
5798 | 341 endif |
342 | |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
343 ## Display nicely formatted complexity. |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
344 printf ("\nFor %s:\n", __f1); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
345 printf (" asymptotic power: %s\n", order); |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
346 printf (" approximate time per operation: %s\n", time); |
5798 | 347 |
348 endif | |
349 | |
5589 | 350 endfunction |
351 | |
13274
b48e11427ac0
speed.m: Fix 2 intermittently failing tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13065
diff
changeset
|
352 |
b48e11427ac0
speed.m: Fix 2 intermittently failing tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13065
diff
changeset
|
353 %% FIXME: Demos with declared functions do not work. See bug #31815. |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
354 %% A workaround has been hacked by not declaring the functions |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
355 %% but using eval to create them in the proper context. |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
356 %% Unfortunately, we can't remove them from the user's workspace |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
357 %% because of another bug (#34497). |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
358 %!demo |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
359 %! fstr_build_orig = cstrcat ( |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
360 %! "function x = build_orig (n)\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
361 %! " ## extend the target vector on the fly\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
362 %! " for i=0:n-1, x([1:100]+i*100) = 1:100; endfor\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
363 %! "endfunction"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
364 %! fstr_build = cstrcat ( |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
365 %! "function x = build (n)\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
366 %! " ## preallocate the target vector\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
367 %! " x = zeros (1, n*100);\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
368 %! " for i=0:n-1, x([1:100]+i*100) = 1:100; endfor\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
369 %! "endfunction"); |
5589 | 370 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
371 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
372 %! disp (fstr_build_orig); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
373 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
374 %! disp (fstr_build); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
375 %! disp ("-----------------------"); |
5589 | 376 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
377 %! ## Eval functions strings to create them in the current context |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
378 %! eval (fstr_build_orig); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
379 %! eval (fstr_build); |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
380 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
381 %! disp ("Preallocated vector test.\nThis takes a little while..."); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
382 %! speed ("build (n)", "", 1000, "build_orig (n)"); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
383 %! clear -f build build_orig |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
384 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
385 %! disp ("Note how much faster it is to pre-allocate a vector."); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
386 %! disp ("Notice the peak speedup ratio."); |
5589 | 387 |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
388 %!demo |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
389 %! fstr_build_orig = cstrcat ( |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
390 %! "function x = build_orig (n)\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
391 %! " for i=0:n-1, x([1:100]+i*100) = 1:100; endfor\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
392 %! "endfunction"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
393 %! fstr_build = cstrcat ( |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
394 %! "function x = build (n)\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
395 %! " idx = [1:100]';\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
396 %! " x = idx(:,ones(1,n));\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
397 %! " x = reshape (x, 1, n*100);\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
398 %! "endfunction"); |
5589 | 399 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
400 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
401 %! disp (fstr_build_orig); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
402 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
403 %! disp (fstr_build); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
404 %! disp ("-----------------------"); |
5589 | 405 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
406 %! ## Eval functions strings to create them in the current context |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
407 %! eval (fstr_build_orig); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
408 %! eval (fstr_build); |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
409 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
410 %! disp ("Vectorized test.\nThis takes a little while..."); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
411 %! speed ("build (n)", "", 1000, "build_orig (n)"); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
412 %! clear -f build build_orig |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
413 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
414 %! disp ("This time, the for loop is done away with entirely."); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
415 %! disp ("Notice how much bigger the speedup is than in example 1."); |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
416 |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
417 %!test |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
418 %! [order, n, T_f1, T_f2] = speed ("airy (x)", "x = rand (n, 10)", [100, 1000]); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
419 %! assert (isstruct (order)); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
420 %! assert (size (order), [1, 1]); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
421 %! assert (fieldnames (order), {"p"; "a"}); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
422 %! assert (isnumeric (n)); |
13274
b48e11427ac0
speed.m: Fix 2 intermittently failing tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13065
diff
changeset
|
423 %! assert (length (n) > 10); |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
424 %! assert (isnumeric (T_f1)); |
13276
d5c8c2fe2eed
speed.m: Further fixes for 2 speed tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13274
diff
changeset
|
425 %! assert (size (T_f1), size (n)); |
13274
b48e11427ac0
speed.m: Fix 2 intermittently failing tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13065
diff
changeset
|
426 %! assert (isnumeric (T_f2)); |
13276
d5c8c2fe2eed
speed.m: Further fixes for 2 speed tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13274
diff
changeset
|
427 %! assert (length (T_f2) > 10); |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
428 |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
429 %% This test is known to fail on operating systems with low resolution timers |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
430 %% such as MinGW |
13283
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
431 %!xtest |
13276
d5c8c2fe2eed
speed.m: Further fixes for 2 speed tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13274
diff
changeset
|
432 %! [order, n, T_f1, T_f2] = speed ("sum (x)", "", [100, 1000], "v = 0; for i = 1:length (x), v += x(i); endfor"); |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
433 %! assert (isstruct (order)); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
434 %! assert (size (order), [1, 1]); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
435 %! assert (fieldnames (order), {"p"; "a"}); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
436 %! assert (isnumeric (n)); |
13274
b48e11427ac0
speed.m: Fix 2 intermittently failing tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13065
diff
changeset
|
437 %! assert (length (n) > 10); |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
438 %! assert (isnumeric (T_f1)); |
13276
d5c8c2fe2eed
speed.m: Further fixes for 2 speed tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13274
diff
changeset
|
439 %! assert (size (T_f1), size (n)); |
13274
b48e11427ac0
speed.m: Fix 2 intermittently failing tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13065
diff
changeset
|
440 %! assert (isnumeric (T_f2)); |
13276
d5c8c2fe2eed
speed.m: Further fixes for 2 speed tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13274
diff
changeset
|
441 %! assert (length (T_f2) > 10); |
13274
b48e11427ac0
speed.m: Fix 2 intermittently failing tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13065
diff
changeset
|
442 |
b48e11427ac0
speed.m: Fix 2 intermittently failing tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13065
diff
changeset
|
443 %% Test input validation |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
444 %!error speed () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
445 %!error speed (1, 2, 3, 4, 5, 6, 7) |
13274
b48e11427ac0
speed.m: Fix 2 intermittently failing tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13065
diff
changeset
|
446 |