Mercurial > octave-antonio
annotate scripts/testfun/speed.m @ 20162:2645f9ef8c88 stable
doc: Update more docstrings to have one sentence summary as first line.
Reviewed specfun, special-matrix, testfun, and time script directories.
* scripts/specfun/expint.m, scripts/specfun/isprime.m,
scripts/specfun/legendre.m, scripts/specfun/primes.m,
scripts/specfun/reallog.m, scripts/specfun/realsqrt.m,
scripts/special-matrix/gallery.m, scripts/special-matrix/hadamard.m,
scripts/special-matrix/hankel.m, scripts/special-matrix/hilb.m,
scripts/special-matrix/invhilb.m, scripts/special-matrix/magic.m,
scripts/special-matrix/pascal.m, scripts/special-matrix/rosser.m,
scripts/special-matrix/toeplitz.m, scripts/special-matrix/vander.m,
scripts/special-matrix/wilkinson.m, scripts/testfun/assert.m,
scripts/testfun/demo.m, scripts/testfun/example.m, scripts/testfun/fail.m,
scripts/testfun/rundemos.m, scripts/testfun/runtests.m,
scripts/testfun/speed.m, scripts/time/asctime.m, scripts/time/calendar.m,
scripts/time/clock.m, scripts/time/ctime.m, scripts/time/datenum.m,
scripts/time/datestr.m, scripts/time/datevec.m, scripts/time/etime.m,
scripts/time/is_leap_year.m, scripts/time/now.m, scripts/time/weekday.m:
Update more docstrings to have one sentence summary as first line.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 03 May 2015 17:00:11 -0700 |
parents | 9fc020886ae9 |
children |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19593
diff
changeset
|
1 ## Copyright (C) 2000-2015 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 |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
24 ## values (@var{n}). |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
25 ## |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
26 ## The @var{n} are log-spaced from 1 to @var{max_n}. For each @var{n}, an |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
27 ## initialization expression (@var{init}) is computed to create any data needed |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
28 ## for the test. If a second expression (@var{f2}) is given then the |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
29 ## execution times of the two expressions are compared. When called without |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
30 ## output arguments the results are printed to stdout and displayed |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
31 ## graphically. |
5589 | 32 ## |
33 ## @table @code | |
34 ## @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
|
35 ## The code expression to evaluate. |
5589 | 36 ## |
37 ## @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
|
38 ## 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
|
39 ## 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
|
40 ## @code{[n1, n2, @dots{}, nk]}. |
5589 | 41 ## |
42 ## @item @var{init} | |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
43 ## Initialization expression for function argument values. Use @var{k} for |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
44 ## the test number and @var{n} for the size of the test. This should compute |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
45 ## values for all variables used by @var{f}. Note that @var{init} will be |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
46 ## 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
|
47 ## 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
|
48 ## @code{@var{x} = randn (@var{n}, 1)}. |
5589 | 49 ## |
50 ## @item @var{f2} | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
51 ## 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
|
52 ## expressions can be directly compared. The default is @code{[]}. |
5589 | 53 ## |
54 ## @item @var{tol} | |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
55 ## 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
|
56 ## @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
|
57 ## 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
|
58 ## @code{eps}. If @var{tol} is @code{Inf}, then no comparison will be made. |
5589 | 59 ## |
5798 | 60 ## @item @var{order} |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
61 ## The time complexity of the expression @math{O(a*n^p)}. This is a |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
62 ## structure with fields @code{a} and @code{p}. |
5589 | 63 ## |
5798 | 64 ## @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
|
65 ## The values @var{n} for which the expression was calculated @strong{AND} |
5798 | 66 ## the execution time was greater than zero. |
5589 | 67 ## |
5798 | 68 ## @item @var{T_f} |
69 ## The nonzero execution times recorded for the expression @var{f} in seconds. | |
70 ## | |
71 ## @item @var{T_f2} | |
72 ## 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
|
73 ## If required, the mean time ratio is simply @code{mean (T_f ./ T_f2)}. |
5798 | 74 ## |
5589 | 75 ## @end table |
76 ## | |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
77 ## The slope of the execution time graph shows the approximate power of the |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
78 ## asymptotic running time @math{O(n^p)}. This power is plotted for the |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
79 ## region over which it is approximated (the latter half of the graph). The |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
80 ## estimated power is not very accurate, but should be sufficient to |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
81 ## determine the general order of an algorithm. It should indicate if, for |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
82 ## example, the implementation is unexpectedly @math{O(n^2)} rather than |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
83 ## @math{O(n)} because it extends a vector each time through the loop rather |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
84 ## than pre-allocating storage. In the current version of Octave, the |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
85 ## following is not the 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 ## | |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
101 ## An attempt is made to approximate the cost of individual operations, but |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
102 ## it is wildly inaccurate. You can improve the stability somewhat by doing |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
103 ## more work for each @code{n}. For example: |
5798 | 104 ## |
105 ## @example | |
8507 | 106 ## speed ("airy(x)", "x = rand (n, 10)", [10000, 100000]) |
5589 | 107 ## @end example |
108 ## | |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
109 ## When comparing two different expressions (@var{f}, @var{f2}), the slope of |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
110 ## 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, |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
112 ## vectorizing an algorithm will not change the slope of the execution time |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
113 ## graph, but will shift it relative to the original. For example: |
5798 | 114 ## |
115 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
116 ## @group |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
117 ## 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
|
118 ## "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
|
119 ## @end group |
5798 | 120 ## @end example |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
121 ## |
11314
87f258202b0f
speed.m: Overhaul documentation string.
Rik <octave@nomad.inbox5.com>
parents:
10846
diff
changeset
|
122 ## 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
|
123 ## 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
|
124 ## 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
|
125 ## lag with varying vector lengths as follows: |
5589 | 126 ## |
127 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
128 ## @group |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
129 ## 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
|
130 ## "xcorr_orig (x, n)", -100*eps) |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
131 ## 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
|
132 ## "xcorr_orig (x, n)", -100*eps) |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
133 ## @end group |
5589 | 134 ## @end example |
135 ## | |
20162
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
136 ## Assuming one of the two versions is in xcorr_orig, this would compare their |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
137 ## speed and their output values. Note that the FFT version is not exact, so |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
138 ## one must specify an acceptable tolerance on the comparison @code{100*eps}. |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
139 ## In this case, the comparison should be computed relatively, as |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
140 ## @code{abs ((@var{x} - @var{y}) ./ @var{y})} rather than absolutely as |
2645f9ef8c88
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
141 ## @code{abs (@var{x} - @var{y})}. |
5589 | 142 ## |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17904
diff
changeset
|
143 ## Type @kbd{example ("speed")} to see some real examples or |
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
|
144 ## @kbd{demo ("speed")} to run them. |
5589 | 145 ## @end deftypefn |
146 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7540
diff
changeset
|
147 ## 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
|
148 function [__order, __test_n, __tnew, __torig] = speed (__f1, __init, __max_n = 100, __f2 = "", __tol = eps) |
6494 | 149 |
150 if (nargin < 1 || nargin > 6) | |
6046 | 151 print_usage (); |
5589 | 152 endif |
6494 | 153 |
154 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
|
155 __init = "x = randn (n, 1)"; |
6494 | 156 endif |
157 | |
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
|
158 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
|
159 __max_n = 100; |
6494 | 160 endif |
5798 | 161 |
162 __numtests = 15; | |
5589 | 163 |
8506 | 164 ## Let user specify range of n. |
6494 | 165 if (isscalar (__max_n)) |
5798 | 166 __min_n = 1; |
6494 | 167 assert (__max_n > __min_n); |
168 __test_n = logspace (0, log10 (__max_n), __numtests); | |
169 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
|
170 [__min_n, __max_n] = deal (__max_n(1), __max_n(2)); |
6494 | 171 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
|
172 assert (__max_n > __min_n); |
6494 | 173 __test_n = logspace (log10 (__min_n), log10 (__max_n), __numtests); |
5798 | 174 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
|
175 assert (all (__max_n > 0)); |
5798 | 176 __test_n = __max_n; |
177 endif | |
8506 | 178 ## Force n to be an integer. |
179 __test_n = unique (round (__test_n)); | |
6494 | 180 assert (__test_n >= 1); |
5589 | 181 |
6494 | 182 __torig = __tnew = zeros (size (__test_n)); |
5589 | 183 |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
184 ## 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
|
185 do_display = (nargout == 0); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
186 |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
187 if (do_display) |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
188 disp (["testing " __f1 "\ninit: " __init]); |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
189 endif |
5589 | 190 |
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
|
191 ## Add semicolon closure to all code fragments in case user has not done so. |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
192 __init(end+1) = ";"; |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
193 __f1(end+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
|
194 if (! isempty (__f2)) |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
195 __f2(end+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
|
196 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
|
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; | |
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
|
202 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
|
203 eval (__f1); |
6494 | 204 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
|
205 eval (__f2); |
6494 | 206 endif |
5589 | 207 |
8506 | 208 ## Run the tests. |
6494 | 209 for k = 1:length (__test_n) |
210 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
|
211 eval (__init); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
212 |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
213 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
|
214 printf ("n%i = %i ", k, n); |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
215 fflush (stdout); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
216 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
|
217 |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
218 eval (["__t = time();" __f1 "__v1=ans; __t = time()-__t;"]); |
5589 | 219 if (__t < 0.25) |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
220 eval (["__t2 = time();" __f1 "__t2 = time()-__t2;"]); |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
221 eval (["__t3 = time();" __f1 "__t3 = time()-__t3;"]); |
6494 | 222 __t = min ([__t, __t2, __t3]); |
5589 | 223 endif |
224 __tnew(k) = __t; | |
225 | |
6494 | 226 if (! isempty (__f2)) |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
227 eval (["__t = time();" __f2 "__v2=ans; __t = time()-__t;"]); |
5589 | 228 if (__t < 0.25) |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
229 eval (["__t2 = time();" __f2 "__t2 = time()-__t2;"]); |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
230 eval (["__t3 = time();" __f2 "__t3 = time()-__t3;"]); |
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 __t = min ([__t, __t2, __t3]); |
5589 | 232 endif |
233 __torig(k) = __t; | |
6494 | 234 if (! isinf(__tol)) |
10549 | 235 assert (__v1, __v2, __tol); |
5589 | 236 endif |
237 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
|
238 |
5798 | 239 endfor |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
240 |
8506 | 241 ## 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
|
242 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
|
243 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
|
244 __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
|
245 __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
|
246 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
|
247 zidx = (__tnew < 100*eps | __torig < 100*eps); |
5798 | 248 __test_n(zidx) = []; |
249 __tnew(zidx) = []; | |
250 __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
|
251 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
|
252 |
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 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
|
254 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
|
255 "error: speed: Choose larger MAX_N or do more work per function evaluation"]); |
5589 | 256 endif |
6494 | 257 |
8506 | 258 ## 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
|
259 tailidx = ceil (length (__test_n)/2):length (__test_n); |
6494 | 260 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
|
261 if (nargout > 0) |
5798 | 262 __order.p = p(1); |
6494 | 263 __order.a = exp (p(2)); |
5798 | 264 endif |
5589 | 265 |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
266 if (do_display) |
6430 | 267 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
|
268 ## Strip semicolon added to code fragments before displaying |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17904
diff
changeset
|
269 __init(end) = ""; |
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17904
diff
changeset
|
270 __f1(end) = ""; |
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
|
271 if (! isempty (__f2)) |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17904
diff
changeset
|
272 __f2(end) = ""; |
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
|
273 endif |
6430 | 274 endif |
5798 | 275 |
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
|
276 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
|
277 |
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 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
|
279 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
|
280 ylabel ("best execution time (ms)"); |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
281 title ({__f1, ["init: " __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
|
282 |
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 elseif (do_display) |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
284 |
6494 | 285 subplot (1, 2, 1); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
286 semilogx (__test_n, __torig./__tnew, |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
287 ["-*r;" strrep(__f1, ";", ".") " / " strrep(__f2, ";", ".") ";"], |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
288 __test_n, __tnew./__torig, |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
289 ["-*g;", strrep(__f2, ";", ".") " / " strrep(__f1, ";", ".") ";"]); |
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
|
290 title ("Speedup Ratio"); |
6494 | 291 xlabel ("test length"); |
292 ylabel ("speedup ratio"); | |
293 | |
294 subplot (1, 2, 2); | |
295 loglog (__test_n, __tnew*1000, | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
296 ["*-g;" strrep(__f1,";",".") ";"], |
10549 | 297 __test_n, __torig*1000, |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
298 ["*-r;" strrep(__f2,";",".") ";"]); |
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
299 title ({"Execution Times", ["init: " __init]}); |
6494 | 300 xlabel ("test length"); |
5589 | 301 ylabel ("best execution time (ms)"); |
6494 | 302 |
5798 | 303 ratio = mean (__torig ./ __tnew); |
6494 | 304 printf ("\n\nMean runtime ratio = %.3g for '%s' vs '%s'\n", |
5798 | 305 ratio, __f2, __f1); |
306 | |
5589 | 307 endif |
5798 | 308 |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
309 if (do_display) |
5798 | 310 |
311 ## 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
|
312 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
|
313 |
5d3d802c772e
speed.m: Overhaul code to fix broken demos and tests (Bug #34234, Bug #31815)
Rik <octave@nomad.inbox5.com>
parents:
13276
diff
changeset
|
314 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
|
315 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
|
316 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
|
317 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
|
318 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
|
319 endif |
6494 | 320 v = polyval (p, log (__test_n(tailidx))); |
321 | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
322 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
|
323 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
|
324 xlabel ("test length"); |
5798 | 325 |
8506 | 326 ## Get base time to 1 digit of accuracy. |
6494 | 327 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
|
328 dt = floor (dt/10^floor (log10 (dt)))*10^floor (log10 (dt)); |
6494 | 329 if (log10 (dt) >= -0.5) |
330 time = sprintf ("%g s", dt); | |
331 elseif (log10 (dt) >= -3.5) | |
332 time = sprintf ("%g ms", dt*1e3); | |
333 elseif (log10 (dt) >= -6.5) | |
334 time = sprintf ("%g us", dt*1e6); | |
335 else | |
336 time = sprintf ("%g ns", dt*1e9); | |
5798 | 337 endif |
338 | |
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
|
339 ## 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
|
340 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
|
341 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
|
342 printf (" approximate time per operation: %s\n", time); |
5798 | 343 |
344 endif | |
345 | |
5589 | 346 endfunction |
347 | |
13274
b48e11427ac0
speed.m: Fix 2 intermittently failing tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13065
diff
changeset
|
348 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
349 ## FIXME: Demos with declared functions do not work. See bug #31815. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
350 ## A workaround has been hacked by not declaring the functions |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
351 ## but using eval to create them in the proper context. |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
352 ## Unfortunately, we can't remove them from the user's workspace |
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
353 ## because of another bug (#34497). |
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 %!demo |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
355 %! fstr_build_orig = cstrcat ( |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
356 %! "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
|
357 %! " ## 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
|
358 %! " 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
|
359 %! "endfunction"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
360 %! fstr_build = cstrcat ( |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
361 %! "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
|
362 %! " ## 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
|
363 %! " 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
|
364 %! " 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
|
365 %! "endfunction"); |
5589 | 366 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
367 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
368 %! disp (fstr_build_orig); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
369 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
370 %! disp (fstr_build); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
371 %! disp ("-----------------------"); |
5589 | 372 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
373 %! ## 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
|
374 %! eval (fstr_build_orig); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
375 %! 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
|
376 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
377 %! 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
|
378 %! 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
|
379 %! 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
|
380 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
381 %! 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
|
382 %! disp ("Notice the peak speedup ratio."); |
5589 | 383 |
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
|
384 %!demo |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
385 %! fstr_build_orig = cstrcat ( |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
386 %! "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
|
387 %! " 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
|
388 %! "endfunction"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
389 %! fstr_build = 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 (n)\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
391 %! " idx = [1:100]';\n", |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
392 %! " 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
|
393 %! " 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
|
394 %! "endfunction"); |
5589 | 395 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
396 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
397 %! disp (fstr_build_orig); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
398 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
399 %! disp (fstr_build); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
400 %! disp ("-----------------------"); |
5589 | 401 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
402 %! ## 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
|
403 %! eval (fstr_build_orig); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
404 %! 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
|
405 %! |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
406 %! 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
|
407 %! 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
|
408 %! 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
|
409 %! disp ("-----------------------"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
410 %! 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
|
411 %! 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
|
412 |
17904
817da22e58ed
speed.m: Change %!test blocks to %!xtest blocks.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
413 ## FIXME: Tests are known to fail on operating systems with low resolution |
817da22e58ed
speed.m: Change %!test blocks to %!xtest blocks.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
414 ## timers such as MinGW. Therefore, tests are made xtests so that false |
817da22e58ed
speed.m: Change %!test blocks to %!xtest blocks.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
415 ## failures are not reported. However, it might be better to either |
817da22e58ed
speed.m: Change %!test blocks to %!xtest blocks.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
416 ## force the tests to do more work, or use %!testif to check the OS. |
817da22e58ed
speed.m: Change %!test blocks to %!xtest blocks.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
417 %!xtest |
13065
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 |
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
|
429 %!xtest |
13276
d5c8c2fe2eed
speed.m: Further fixes for 2 speed tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13274
diff
changeset
|
430 %! [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
|
431 %! assert (isstruct (order)); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
432 %! assert (size (order), [1, 1]); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
433 %! assert (fieldnames (order), {"p"; "a"}); |
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
434 %! assert (isnumeric (n)); |
13274
b48e11427ac0
speed.m: Fix 2 intermittently failing tests (Bug #34234)
Rik <octave@nomad.inbox5.com>
parents:
13065
diff
changeset
|
435 %! assert (length (n) > 10); |
13065
1ca3cde15b18
codepsprint: tests for testfun functions
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
436 %! 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
|
437 %! 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
|
438 %! 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
|
439 %! 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
|
440 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
441 ## Test input validation |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
442 %!error speed () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14237
diff
changeset
|
443 %!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
|
444 |