annotate scripts/testfun/speed.m @ 8506:bc982528de11

comment style fixes
author John W. Eaton <jwe@octave.org>
date Tue, 13 Jan 2009 11:56:00 -0500
parents cf59d542f33e
children cadc73247d65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 ## Paul Kienzle
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
3 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
4 ## This file is part of Octave.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
5 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
7 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
9 ## your option) any later version.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
10 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
14 ## General Public License for more details.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
15 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
19
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
21 ## @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
22 ## @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
23 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
24 ## 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
25 ## 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
26 ## 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
27 ## 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
28 ## 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
29 ## without output arguments the results are presented graphically.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
30 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
31 ## @table @code
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
32 ## @item @var{f}
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
33 ## The expression to evaluate.
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
34 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
35 ## @item @var{max_n}
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
36 ## 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
37 ## 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
38 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
39 ## @item @var{init}
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
40 ## Initialization expression for function argument values. Use @var{k}
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
41 ## 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
42 ## 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
43 ## 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
44 ## 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
45 ## randn (@var{n}, 1);}.
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
46 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
47 ## @item @var{f2}
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
48 ## 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
49 ## can be compared. Default is @code{[]}.
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
50 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
51 ## @item @var{tol}
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
52 ## 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
53 ## results of expression @var{f} and expression @var{f2}. Otherwise,
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
54 ## 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
55 ## should produce a value @var{v2}, and these shall be compared using
6429
6ff2d413cf58 [project @ 2007-03-21 20:13:03 by dbateman]
dbateman
parents: 6046
diff changeset
56 ## @code{assert(@var{v},@var{v2},@var{tol})}. If @var{tol} is positive,
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6494
diff changeset
57 ## the tolerance is assumed to be absolute. If @var{tol} is negative,
6429
6ff2d413cf58 [project @ 2007-03-21 20:13:03 by dbateman]
dbateman
parents: 6046
diff changeset
58 ## the tolerance is assumed to be relative. The default is @code{eps}.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
59 ##
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
60 ## @item @var{order}
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
61 ## 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
62 ## 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
63 ##
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
64 ## @item @var{n}
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6494
diff changeset
65 ## The values @var{n} for which the expression was calculated and
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
66 ## the execution time was greater than zero.
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
67 ##
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
68 ## @item @var{T_f}
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{f} in seconds.
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
70 ##
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
71 ## @item @var{T_f2}
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
72 ## 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
73 ## 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
74 ##
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
75 ## @end table
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
76 ##
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
77 ## 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
78 ## 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
79 ## 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
80 ## (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
81 ## very accurate, but should be sufficient to determine the
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
82 ## 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
83 ## example your implementation is unexpectedly @code{O(n^2)}
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
84 ## 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
85 ## 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
86 ## 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
87 ## the following is not the expected @code{O(n)}:
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
88 ##
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
89 ## @example
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
90 ## 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
91 ## @end example
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
92 ##
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
93 ## 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
94 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
95 ## @example
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
96 ## 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
97 ## "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
98 ## @end example
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
99 ##
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
100 ## 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
101 ## 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
102 ## 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
103 ## example:
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
104 ##
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
105 ## @example
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
106 ## speed("airy(x)", "x=rand(n,10)", [10000,100000])
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
107 ## @end example
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
108 ##
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
109 ## 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
110 ## 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
111 ## is faster. Better algorithms have a shallow slope. Generally,
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
112 ## 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
113 ## 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
114 ## example:
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
115 ##
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
116 ## @example
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
117 ## speed("v=sum(x)", "", [10000,100000], ...
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
118 ## "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
119 ## @end example
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
120 ##
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
121 ## 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
122 ## 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
123 ## 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
124 ## vector lengths as follows:
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
125 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
126 ## @example
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
127 ## speed("v=xcorr(x,n)", "x=rand(128,1);", 100, ...
6429
6ff2d413cf58 [project @ 2007-03-21 20:13:03 by dbateman]
dbateman
parents: 6046
diff changeset
128 ## "v2=xcorr_orig(x,n)", -100*eps)
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
129 ## speed("v=xcorr(x,15)", "x=rand(20+n,1);", 100, ...
6429
6ff2d413cf58 [project @ 2007-03-21 20:13:03 by dbateman]
dbateman
parents: 6046
diff changeset
130 ## "v2=xcorr_orig(x,n)", -100*eps)
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
131 ## @end example
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
132 ##
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6494
diff changeset
133 ## Assuming one of the two versions is in @var{xcorr_orig}, this
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
134 ## 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
135 ## 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
136 ## 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
137 ## 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
138 ## absolutely as @code{abs(@var{x} - @var{y})}.
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
139 ##
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
140 ## 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
141 ## 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
142 ## @code{demo('speed')}. Instead use, @code{eval(example('speed',1))}
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
143 ## and @code{eval(example('speed',2))}.
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
144 ## @end deftypefn
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
145
8202
cf59d542f33e replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents: 7540
diff changeset
146 ## FIXME: 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
147 function [__order, __test_n, __tnew, __torig] ...
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
148 = speed (__f1, __init, __max_n, __f2, __tol)
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
149
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
150 if (nargin < 1 || nargin > 6)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5798
diff changeset
151 print_usage ();
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
152 endif
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
153
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
154 if (nargin < 2 || isempty (__init))
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
155 __init = "x = randn(n, 1);";
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
156 endif
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
157
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
158 if (nargin < 3 || isempty (__max_n))
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
159 __max_n = 100;
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
160 endif
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
161
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
162 if (nargin < 4)
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
163 __f2 = [];
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
164 endif
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
165
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
166 if (nargin < 5 || isempty (__tol))
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
167 __tol = eps;
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
168 endif
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
169
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
170 __numtests = 15;
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
171
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
172 ## Let user specify range of n.
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
173 if (isscalar (__max_n))
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
174 __min_n = 1;
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
175 assert (__max_n > __min_n);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
176 __test_n = logspace (0, log10 (__max_n), __numtests);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
177 elseif (length (__max_n) == 2)
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
178 __min_n = __max_n(1);
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
179 __max_n = __max_n(2);
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
180 assert (__min_n >= 1);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
181 __test_n = logspace (log10 (__min_n), log10 (__max_n), __numtests);
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
182 else
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
183 __test_n = __max_n;
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
184 endif
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
185 ## Force n to be an integer.
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
186 __test_n = unique (round (__test_n));
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
187 assert (__test_n >= 1);
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
188
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
189 __torig = __tnew = zeros (size (__test_n));
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
190
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
191 disp (cstrcat ("testing ", __f1, "\ninit: ", __init));
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
192
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
193 ## 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
194 ## test_n(1); first have to initialize the args though.
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
195 n = 1;
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
196 k = 0;
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
197 eval (cstrcat (__init, ";"));
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
198 if (! isempty (__f2))
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
199 eval (cstrcat (__f2, ";"));
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
200 endif
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
201 eval (cstrcat (__f1, ";"));
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
202
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
203 ## Run the tests.
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
204 for k = 1:length (__test_n)
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
205 n = __test_n(k);
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
206 eval (cstrcat (__init, ";"));
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
207
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
208 printf ("n%i=%i ",k, n);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
209 fflush (stdout);
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
210 eval (cstrcat ("__t=time();", __f1, "; __v1=ans; __t = time()-__t;"));
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
211 if (__t < 0.25)
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
212 eval (cstrcat ("__t2=time();", __f1, "; __t2 = time()-__t2;"));
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
213 eval (cstrcat ("__t3=time();", __f1, "; __t3 = time()-__t3;"));
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
214 __t = min ([__t, __t2, __t3]);
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
215 endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
216 __tnew(k) = __t;
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
217
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
218 if (! isempty (__f2))
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
219 eval (cstrcat ("__t=time();", __f2, "; __v2=ans; __t = time()-__t;"));
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
220 if (__t < 0.25)
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
221 eval (cstrcat ("__t2=time();", __f2, "; __t2 = time()-__t2;"));
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
222 eval (cstrcat ("__t3=time();", __f2, "; __t3 = time()-__t3;"));
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
223 endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
224 __torig(k) = __t;
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
225 if (! isinf(__tol))
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
226 assert (__v1, __v2, __tol);
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
227 endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
228 endif
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
229 endfor
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
230
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
231 ## Drop times of zero.
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
232 if (! isempty (__f2))
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
233 zidx = (__tnew < 100*eps | __torig < 100*eps);
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
234 __test_n(zidx) = [];
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
235 __tnew(zidx) = [];
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
236 __torig(zidx) = [];
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
237 else
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
238 zidx = (__tnew < 100*eps);
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
239 __test_n(zidx) = [];
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
240 __tnew(zidx) = [];
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
241 endif
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
242
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
243 ## Approximate time complexity and return it if requested.
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
244 tailidx = ceil(length(__test_n)/2):length(__test_n);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
245 p = polyfit (log (__test_n(tailidx)), log (__tnew(tailidx)), 1);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
246 if (nargout > 0)
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
247 __order.p = p(1);
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
248 __order.a = exp (p(2));
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
249 endif
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
250
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
251 ## Plot the data if no output is requested.
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
252 doplot = (nargout == 0);
6430
215b141470b4 [project @ 2007-03-21 20:58:08 by dbateman]
dbateman
parents: 6429
diff changeset
253
215b141470b4 [project @ 2007-03-21 20:58:08 by dbateman]
dbateman
parents: 6429
diff changeset
254 if (doplot)
215b141470b4 [project @ 2007-03-21 20:58:08 by dbateman]
dbateman
parents: 6429
diff changeset
255 figure;
215b141470b4 [project @ 2007-03-21 20:58:08 by dbateman]
dbateman
parents: 6429
diff changeset
256 endif
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
257
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
258 if (doplot && ! isempty (__f2))
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
259 subplot (1, 2, 1);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
260 semilogx (__test_n, __torig./__tnew,
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
261 cstrcat ("-*r;", strrep (__f1, ";", "."), "/",
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
262 strrep (__f2, ";", "."), ";"),
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
263 __test_n, __tnew./__torig,
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
264 cstrcat ("-*g;", strrep (__f2, ";", "."), "/",
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
265 strrep (__f1, ";", "."), ";"));
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
266 xlabel ("test length");
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
267 title (__f1);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
268 ylabel ("speedup ratio");
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
269
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
270 subplot (1, 2, 2);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
271 loglog (__test_n, __tnew*1000,
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
272 cstrcat ("*-g;", strrep (__f1, ";", "."), ";" ),
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
273 __test_n, __torig*1000,
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
274 cstrcat ("*-r;", strrep (__f2,";","."), ";"));
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
275
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
276 xlabel ("test length");
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
277 ylabel ("best execution time (ms)");
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
278 title (cstrcat ("init: ", __init));
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
279
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
280 ratio = mean (__torig ./ __tnew);
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
281 printf ("\n\nMean runtime ratio = %.3g for '%s' vs '%s'\n",
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
282 ratio, __f2, __f1);
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
283
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
284 elseif (doplot)
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
285
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
286 loglog (__test_n, __tnew*1000, "*-g;execution time;");
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
287 xlabel ("test length");
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
288 ylabel ("best execution time (ms)");
7540
3422f39573b1 strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
289 title (cstrcat (__f1, " init: ", __init));
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
290
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
291 endif
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
292
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
293 if (doplot)
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
294
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
295 ## Plot time complexity approximation (using milliseconds).
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
296 order = sprintf ("O(n^%g)", round (10*p(1))/10);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
297 v = polyval (p, log (__test_n(tailidx)));
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
298
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
299 loglog (__test_n(tailidx), exp(v)*1000, sprintf ("b;%s;", order));
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
300
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
301 ## Get base time to 1 digit of accuracy.
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
302 dt = exp (p(2));
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
303 dt = floor (dt/10^floor(log10(dt)))*10^floor(log10(dt));
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
304 if (log10 (dt) >= -0.5)
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
305 time = sprintf ("%g s", dt);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
306 elseif (log10 (dt) >= -3.5)
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
307 time = sprintf ("%g ms", dt*1e3);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
308 elseif (log10 (dt) >= -6.5)
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
309 time = sprintf ("%g us", dt*1e6);
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
310 else
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
311 time = sprintf ("%g ns", dt*1e9);
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
312 endif
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
313
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
314 ## Display nicely formatted complexity.
6494
76a1a953533d [project @ 2007-04-05 16:09:03 by jwe]
jwe
parents: 6430
diff changeset
315 printf ("\nFor %s:\n", __f1);
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
316 printf (" asymptotic power: %s\n", order);
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
317 printf (" approximate time per operation: %s\n", time);
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
318
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
319 endif
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5589
diff changeset
320
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
321 endfunction
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
322
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
323 %!demo if 1
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
324 %! function x = build_orig(n)
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
325 %! ## extend the target vector on the fly
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
326 %! 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
327 %! endfunction
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
328 %! function x = build(n)
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
329 %! ## preallocate the target vector
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
330 %! x = zeros(1, n*10);
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
331 %! try
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
332 %! if (prefer_column_vectors), x = x.'; endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
333 %! catch
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
334 %! end
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
335 %! 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
336 %! endfunction
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
337 %!
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
338 %! disp("-----------------------");
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
339 %! type build_orig;
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
340 %! disp("-----------------------");
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
341 %! type build;
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
342 %! disp("-----------------------");
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
343 %!
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
344 %! disp("Preallocated vector test.\nThis takes a little while...");
6429
6ff2d413cf58 [project @ 2007-03-21 20:13:03 by dbateman]
dbateman
parents: 6046
diff changeset
345 %! speed('build(n)', '', 1000, 'build_orig(n)');
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
346 %! clear build build_orig
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
347 %! 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
348 %! disp("Notice the peak speedup ratio.");
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
349 %! endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
350
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
351 %!demo if 1
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
352 %! function x = build_orig(n)
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
353 %! 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
354 %! endfunction
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
355 %! function x = build(n)
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
356 %! idx = [1:10]';
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
357 %! x = idx(:,ones(1,n));
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
358 %! x = reshape(x, 1, n*10);
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
359 %! try
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
360 %! if (prefer_column_vectors), x = x.'; endif
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
361 %! catch
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
362 %! end
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
363 %! endfunction
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
364 %!
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
365 %! disp("-----------------------");
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
366 %! type build_orig;
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
367 %! disp("-----------------------");
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
368 %! type build;
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
369 %! disp("-----------------------");
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
370 %!
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
371 %! disp("Vectorized test. This takes a little while...");
6429
6ff2d413cf58 [project @ 2007-03-21 20:13:03 by dbateman]
dbateman
parents: 6046
diff changeset
372 %! speed('build(n)', '', 1000, 'build_orig(n)');
5589
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
373 %! clear build build_orig
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
374 %! disp("-----------------------");
f812a0680d05 [project @ 2006-01-06 00:14:42 by jwe]
jwe
parents:
diff changeset
375 %! 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
376 %! 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
377 %! endif