comparison doc/interpreter/tips.txi @ 10828:322f43e0e170

Grammarcheck .txi documentation files.
author Rik <octave@nomad.inbox5.com>
date Wed, 28 Jul 2010 12:45:04 -0700
parents 5b68000faac1
children a4f482e66b65
comparison
equal deleted inserted replaced
10827:228cd18455a6 10828:322f43e0e170
84 Here are some ways of improving the execution speed of Octave programs. 84 Here are some ways of improving the execution speed of Octave programs.
85 85
86 @itemize @bullet 86 @itemize @bullet
87 @item 87 @item
88 Vectorize loops. For instance, rather than 88 Vectorize loops. For instance, rather than
89
89 @example 90 @example
90 @group 91 @group
91 for i = 1:n-1 92 for i = 1:n-1
92 a(i) = b(i+1) - b(i); 93 a(i) = b(i+1) - b(i);
93 endfor 94 endfor
98 99
99 @example 100 @example
100 a = b(2:n) - b(1:n-1); 101 a = b(2:n) - b(1:n-1);
101 @end example 102 @end example
102 103
103 This is especially important for loops with "cheap" bodies. Often it suffices to vectorize 104 This is especially important for loops with "cheap" bodies. Often it suffices
104 just the innermost loop to get acceptable performance. A general rule of thumb is that the 105 to vectorize just the innermost loop to get acceptable performance. A general
105 "order" of the vectorized body should be greater or equal to the "order" of the enclosing loop. 106 rule of thumb is that the "order" of the vectorized body should be greater or
106 107 equal to the "order" of the enclosing loop.
107 @item 108
108 Use built-in and library functions if possible. Built-in and compiled functions are very fast. 109 @item
109 Even with a m-file library function, chances are good that it is already optimized, or will be 110 Use built-in and library functions if possible. Built-in and compiled functions
110 optimized more in a future release. 111 are very fast. Even with a m-file library function, chances are good that it is
112 already optimized, or will be optimized more in a future release.
111 113
112 For instance, even better than 114 For instance, even better than
113 115
114 @example 116 @example
115 a = b(2:n) - b(1:n-1); 117 a = b(2:n) - b(1:n-1);
121 a = diff (b); 123 a = diff (b);
122 @end example 124 @end example
123 125
124 126
125 @item 127 @item
126 Avoid computing costly intermediate results multiple times. Octave currently 128 Avoid computing costly intermediate results multiple times. Octave currently
127 does not eliminate common subexpressions. 129 does not eliminate common subexpressions.
128 Also, certain internal computation results are cached for variables. 130 Also, certain internal computation results are cached for variables.
129 For instance, if a matrix variable is used multiple times as an index, 131 For instance, if a matrix variable is used multiple times as an index,
130 checking the indices (and internal conversion to integers) is only done once. 132 checking the indices (and internal conversion to integers) is only done once.
131 133
166 In rare cases, however, a lazily copied slice outlasts its parent, in which 168 In rare cases, however, a lazily copied slice outlasts its parent, in which
167 case it becomes orphaned, still occupying unnecessarily more memory than needed. 169 case it becomes orphaned, still occupying unnecessarily more memory than needed.
168 To provide a remedy working in most real cases, 170 To provide a remedy working in most real cases,
169 Octave checks for orphaned lazy slices at certain situations, when a value 171 Octave checks for orphaned lazy slices at certain situations, when a value
170 is stored into a "permanent" location, such as a named variable or cell or 172 is stored into a "permanent" location, such as a named variable or cell or
171 struct element, and possibly economizes them. For example 173 struct element, and possibly economizes them. For example:
172 174
173 @example 175 @example
174 @group 176 @group
175 a = zeros (1000); # create a 1000x1000 matrix 177 a = zeros (1000); # create a 1000x1000 matrix
176 b = a(:,10:100); # lazy slice 178 b = a(:,10:100); # lazy slice
178 c@{1@} = b; # b is reallocated at this point 180 c@{1@} = b; # b is reallocated at this point
179 @end group 181 @end group
180 @end example 182 @end example
181 183
182 @item 184 @item
183 Avoid deep recursion. Function calls to m-file functions carry a relatively significant overhead, 185 Avoid deep recursion. Function calls to m-file functions carry a relatively
184 so rewriting a recursion as a loop often helps. Also, note that the maximum level of recursion is 186 significant overhead, so rewriting a recursion as a loop often helps. Also,
185 limited. 187 note that the maximum level of recursion is limited.
186 188
187 @item 189 @item
188 Avoid resizing matrices unnecessarily. When building a single result 190 Avoid resizing matrices unnecessarily. When building a single result
189 matrix from a series of calculations, set the size of the result matrix 191 matrix from a series of calculations, set the size of the result matrix
190 first, then insert values into it. Write 192 first, then insert values into it. Write
210 result = [ result, new_value() ]; 212 result = [ result, new_value() ];
211 endfor 213 endfor
212 @end group 214 @end group
213 @end example 215 @end example
214 216
215 Sometimes the number of items can't be computed in advance, and stack-like operations 217 Sometimes the number of items can't be computed in advance, and stack-like
216 are needed. When elements are being repeatedly inserted at/removed from the end of an 218 operations are needed. When elements are being repeatedly inserted at/removed
217 array, Octave detects it as stack usage and attempts to use a smarter memory management 219 from the end of an array, Octave detects it as stack usage and attempts to use a
218 strategy pre-allocating the array in bigger chunks. Likewise works for cell and 220 smarter memory management strategy pre-allocating the array in bigger chunks.
219 struct arrays. 221 Likewise works for cell and struct arrays.
220 222
221 @example 223 @example
222 @group 224 @group
223 a = []; 225 a = [];
224 while (condition) 226 while (condition)
246 v = cellfun (@@det, a); # faster 248 v = cellfun (@@det, a); # faster
247 @end group 249 @end group
248 @end example 250 @end example
249 251
250 @item 252 @item
251 Octave includes a number of other functions that can replace common types of loops, 253 Octave includes a number of other functions that can replace common types of
252 including @code{bsxfun}, @code{arrayfun}, @code{structfun}, @code{accumarray}. 254 loops, including @code{bsxfun}, @code{arrayfun}, @code{structfun},
253 These functions can take an arbitrary function as a handle. 255 @code{accumarray}. These functions can take an arbitrary function as a handle.
254 Be sure to get familiar with them if you want to become an Octave expert. 256 Be sure to get familiar with them if you want to become an Octave expert.
255 257
256 @item 258 @item
257 Avoid calling @code{eval} or @code{feval} excessively, because 259 Avoid calling @code{eval} or @code{feval} excessively, because
258 they require Octave to parse input or look up the name of a function in 260 they require Octave to parse input or look up the name of a function in
533 535
534 @table @asis 536 @table @asis
535 @item -*- @nospell{texinfo} -*- 537 @item -*- @nospell{texinfo} -*-
536 This string signals Octave that the following text is in Texinfo format, 538 This string signals Octave that the following text is in Texinfo format,
537 and should be the first part of any help string in Texinfo format. 539 and should be the first part of any help string in Texinfo format.
540
538 @item @@deftypefn@{class@} @dots{} @@end deftypefn 541 @item @@deftypefn@{class@} @dots{} @@end deftypefn
539 The entire help string should be enclosed within the block defined by 542 The entire help string should be enclosed within the block defined by
540 deftypefn. 543 deftypefn.
544
541 @item @@cindex index term 545 @item @@cindex index term
542 This generates an index entry, and can be useful when the function is 546 This generates an index entry, and can be useful when the function is
543 included as part of a larger piece of documentation. It is ignored 547 included as part of a larger piece of documentation. It is ignored
544 within Octave's help viewer. Only one index term may appear per line 548 within Octave's help viewer. Only one index term may appear per line
545 but multiple @@cindex lines are valid if the function should be 549 but multiple @@cindex lines are valid if the function should be
546 filed under different terms. 550 filed under different terms.
551
547 @item @@var@{variable@} 552 @item @@var@{variable@}
548 All variables should be marked with this macro. The markup of variables 553 All variables should be marked with this macro. The markup of variables
549 is then changed appropriately for display. 554 is then changed appropriately for display.
555
550 @item @@code@{sample of code@} 556 @item @@code@{sample of code@}
551 All samples of code should be marked with this macro for the same 557 All samples of code should be marked with this macro for the same
552 reasons as the @@var macro. 558 reasons as the @@var macro.
559
553 @item @@seealso@{function2@} 560 @item @@seealso@{function2@}
554 This is a comma separated list of function names that allows cross 561 This is a comma separated list of function names that allows cross
555 referencing from one function documentation string to another. 562 referencing from one function documentation string to another.
556 @end table 563 @end table
557 564