annotate scripts/testfun/speed.m @ 6046:34f96dd5441b

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