1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
3
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
5
<title>GNU Octave: Miscellaneous Techniques</title>
7
<meta name="description" content="GNU Octave: Miscellaneous Techniques">
8
<meta name="keywords" content="GNU Octave: Miscellaneous Techniques">
9
<meta name="resource-type" content="document">
10
<meta name="distribution" content="global">
11
<meta name="Generator" content="makeinfo">
12
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
13
<link href="index.html#Top" rel="start" title="Top">
14
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
15
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
16
<link href="Vectorization-and-Faster-Code-Execution.html#Vectorization-and-Faster-Code-Execution" rel="up" title="Vectorization and Faster Code Execution">
17
<link href="Examples.html#Examples" rel="next" title="Examples">
18
<link href="JIT-Compiler.html#JIT-Compiler" rel="prev" title="JIT Compiler">
19
<style type="text/css">
21
a.summary-letter {text-decoration: none}
22
blockquote.smallquotation {font-size: smaller}
23
div.display {margin-left: 3.2em}
24
div.example {margin-left: 3.2em}
25
div.indentedblock {margin-left: 3.2em}
26
div.lisp {margin-left: 3.2em}
27
div.smalldisplay {margin-left: 3.2em}
28
div.smallexample {margin-left: 3.2em}
29
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
30
div.smalllisp {margin-left: 3.2em}
31
kbd {font-style:oblique}
32
pre.display {font-family: inherit}
33
pre.format {font-family: inherit}
34
pre.menu-comment {font-family: serif}
35
pre.menu-preformatted {font-family: serif}
36
pre.smalldisplay {font-family: inherit; font-size: smaller}
37
pre.smallexample {font-size: smaller}
38
pre.smallformat {font-family: inherit; font-size: smaller}
39
pre.smalllisp {font-size: smaller}
40
span.nocodebreak {white-space:nowrap}
41
span.nolinebreak {white-space:nowrap}
42
span.roman {font-family:serif; font-weight:normal}
43
span.sansserif {font-family:sans-serif; font-weight:normal}
44
ul.no-bullet {list-style: none}
51
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
52
<a name="Miscellaneous-Techniques"></a>
55
Next: <a href="Examples.html#Examples" accesskey="n" rel="next">Examples</a>, Previous: <a href="JIT-Compiler.html#JIT-Compiler" accesskey="p" rel="prev">JIT Compiler</a>, Up: <a href="Vectorization-and-Faster-Code-Execution.html#Vectorization-and-Faster-Code-Execution" accesskey="u" rel="up">Vectorization and Faster Code Execution</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
58
<a name="Miscellaneous-Techniques-1"></a>
59
<h3 class="section">19.6 Miscellaneous Techniques</h3>
60
<a name="index-execution-speed"></a>
61
<a name="index-speedups"></a>
62
<a name="index-optimization"></a>
64
<p>Here are some other ways of improving the execution speed of Octave
68
<li> Avoid computing costly intermediate results multiple times.
69
Octave currently does not eliminate common subexpressions. Also, certain
70
internal computation results are cached for variables. For instance, if
71
a matrix variable is used multiple times as an index, checking the
72
indices (and internal conversion to integers) is only done once.
74
</li><li> Be aware of lazy copies (copy-on-write).
75
<a name="index-copy_002don_002dwrite"></a>
76
<a name="index-COW"></a>
77
<a name="index-memory-management"></a>
78
When a copy of an object is created, the data is not immediately copied, but
79
rather shared. The actual copying is postponed until the copied data needs to
80
be modified. For example:
83
<pre class="example">a = zeros (1000); # create a 1000x1000 matrix
84
b = a; # no copying done here
85
b(1) = 1; # copying done here
88
<p>Lazy copying applies to whole Octave objects such as matrices, cells,
89
struct, and also individual cell or struct elements (not array
92
<p>Additionally, index expressions also use lazy copying when Octave can
93
determine that the indexed portion is contiguous in memory. For example:
96
<pre class="example">a = zeros (1000); # create a 1000x1000 matrix
97
b = a(:,10:100); # no copying done here
98
b = a(10:100,:); # copying done here
101
<p>This applies to arrays (matrices), cell arrays, and structs indexed
102
using ‘<samp>()</samp>’. Index expressions generating comma-separated lists can also
103
benefit from shallow copying in some cases. In particular, when <var>a</var> is a
104
struct array, expressions like <code>{a.x}, {a(:,2).x}</code> will use lazy
105
copying, so that data can be shared between a struct array and a cell array.
107
<p>Most indexing expressions do not live longer than their parent
108
objects. In rare cases, however, a lazily copied slice outlasts its
109
parent, in which case it becomes orphaned, still occupying unnecessarily
110
more memory than needed. To provide a remedy working in most real cases,
111
Octave checks for orphaned lazy slices at certain situations, when a
112
value is stored into a "permanent" location, such as a named variable or
113
cell or struct element, and possibly economizes them. For example:
115
<div class="example">
116
<pre class="example">a = zeros (1000); # create a 1000x1000 matrix
117
b = a(:,10:100); # lazy slice
118
a = []; # the original "a" array is still allocated
119
c{1} = b; # b is reallocated at this point
122
</li><li> Avoid deep recursion.
123
Function calls to m-file functions carry a relatively significant overhead, so
124
rewriting a recursion as a loop often helps. Also, note that the maximum level
125
of recursion is limited.
127
</li><li> Avoid resizing matrices unnecessarily.
128
When building a single result matrix from a series of calculations, set the
129
size of the result matrix first, then insert values into it. Write
131
<div class="example">
132
<pre class="example">result = zeros (big_n, big_m)
133
for i = over:and_over
136
result(ridx, cidx) = new_value ();
142
<div class="example">
143
<pre class="example">result = [];
144
for i = ever:and_ever
145
result = [ result, new_value() ];
149
<p>Sometimes the number of items can not be computed in advance, and
150
stack-like operations are needed. When elements are being repeatedly
151
inserted or removed from the end of an array, Octave detects it as stack
152
usage and attempts to use a smarter memory management strategy by
153
pre-allocating the array in bigger chunks. This strategy is also applied
154
to cell and struct arrays.
156
<div class="example">
157
<pre class="example">a = [];
160
a(end+1) = value; # "push" operation
162
a(end) = []; # "pop" operation
167
</li><li> Avoid calling <code>eval</code> or <code>feval</code> excessively.
168
Parsing input or looking up the name of a function in the symbol table are
169
relatively expensive operations.
171
<p>If you are using <code>eval</code> merely as an exception handling mechanism, and not
172
because you need to execute some arbitrary text, use the <code>try</code>
173
statement instead. See <a href="The-try-Statement.html#The-try-Statement">The try Statement</a>.
175
</li><li> Use <code>ignore_function_time_stamp</code> when appropriate.
176
If you are calling lots of functions, and none of them will need to change
177
during your run, set the variable <code>ignore_function_time_stamp</code> to
178
<code>"all"</code>. This will stop Octave from checking the time stamp of a function
179
file to see if it has been updated while the program is being run.
185
Next: <a href="Examples.html#Examples" accesskey="n" rel="next">Examples</a>, Previous: <a href="JIT-Compiler.html#JIT-Compiler" accesskey="p" rel="prev">JIT Compiler</a>, Up: <a href="Vectorization-and-Faster-Code-Execution.html#Vectorization-and-Faster-Code-Execution" accesskey="u" rel="up">Vectorization and Faster Code Execution</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>