File: Miscellaneous-Techniques.html

package info (click to toggle)
octave 10.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 145,388 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 660; xml: 192
file content (173 lines) | stat: -rw-r--r-- 7,901 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Miscellaneous Techniques (GNU Octave (version 10.3.0))</title>

<meta name="description" content="Miscellaneous Techniques (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Miscellaneous Techniques (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Vectorization-and-Faster-Code-Execution.html" rel="up" title="Vectorization and Faster Code Execution">
<link href="Examples.html" rel="next" title="Examples">
<link href="Memoization.html" rel="prev" title="Memoization">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
ul.mark-bullet {list-style-type: disc}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<div class="section-level-extent" id="Miscellaneous-Techniques">
<div class="nav-panel">
<p>
Next: <a href="Examples.html" accesskey="n" rel="next">Examples</a>, Previous: <a href="Memoization.html" accesskey="p" rel="prev">Memoization</a>, Up: <a href="Vectorization-and-Faster-Code-Execution.html" accesskey="u" rel="up">Vectorization and Faster Code Execution</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h3 class="section" id="Miscellaneous-Techniques-1"><span>19.6 Miscellaneous Techniques<a class="copiable-link" href="#Miscellaneous-Techniques-1"> &para;</a></span></h3>
<a class="index-entry-id" id="index-execution-speed"></a>
<a class="index-entry-id" id="index-speedups"></a>
<a class="index-entry-id" id="index-optimization"></a>

<p>Here are some other ways of improving the execution speed of Octave programs.
</p>
<ul class="itemize mark-bullet">
<li>Avoid computing costly intermediate results multiple times.
Octave currently does not eliminate common subexpressions.  Also, certain
internal computation results are cached for variables.  For instance, if
a matrix variable is used multiple times as an index, checking the
indices (and internal conversion to integers) is only done once.

</li><li>Be aware of lazy copies (copy-on-write).
<a class="index-entry-id" id="index-copy_002don_002dwrite"></a>
<a class="index-entry-id" id="index-COW"></a>
<a class="index-entry-id" id="index-memory-management"></a>
When a copy of an object is created, the data is not immediately copied, but
rather shared.  The actual copying is postponed until the copied data needs to
be modified.  For example:

<div class="example">
<div class="group"><pre class="example-preformatted">a = zeros (1000); # create a 1000x1000 matrix
b = a; # no copying done here
b(1) = 1; # copying done here
</pre></div></div>

<p>Lazy copying applies to whole Octave objects such as matrices, cells,
struct, and also individual cell or struct elements (not array
elements).
</p>
<p>Additionally, index expressions also use lazy copying when Octave can
determine that the indexed portion is contiguous in memory.  For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">a = zeros (1000); # create a 1000x1000 matrix
b = a(:,10:100);  # no copying done here
b = a(10:100,:);  # copying done here
</pre></div></div>

<p>This applies to arrays (matrices), cell arrays, and structs indexed
using &lsquo;<samp class="samp">()</samp>&rsquo;.  Index expressions generating comma-separated lists can also
benefit from shallow copying in some cases.  In particular, when <var class="var">a</var> is a
struct array, expressions like <code class="code">{a.x}, {a(:,2).x}</code> will use lazy
copying, so that data can be shared between a struct array and a cell array.
</p>
<p>Most indexing expressions do not live longer than their parent
objects.  In rare cases, however, a lazily copied slice outlasts its
parent, in which case it becomes orphaned, still occupying unnecessarily
more memory than needed.  To provide a remedy working in most real cases,
Octave checks for orphaned lazy slices at certain situations, when a
value is stored into a &quot;permanent&quot; location, such as a named variable or
cell or struct element, and possibly economizes them.  For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">a = zeros (1000); # create a 1000x1000 matrix
b = a(:,10:100);  # lazy slice
a = []; # the original &quot;a&quot; array is still allocated
c{1} = b; # b is reallocated at this point
</pre></div></div>

</li><li>Avoid deep recursion.
Function calls to m-file functions carry a relatively significant overhead, so
rewriting a recursion as a loop often helps.  Also, note that the maximum level
of recursion is limited.

</li><li>Avoid resizing matrices unnecessarily.
When building a single result matrix from a series of calculations, set the
size of the result matrix first, then insert values into it.  Write

<div class="example">
<div class="group"><pre class="example-preformatted">result = zeros (big_n, big_m)
for i = over:and_over
  ridx = ...
  cidx = ...
  result(ridx, cidx) = new_value ();
endfor
</pre></div></div>

<p>instead of
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">result = [];
for i = ever:and_ever
  result = [ result, new_value() ];
endfor
</pre></div></div>

<p>Sometimes the number of items can not be computed in advance, and
stack-like operations are needed.  When elements are being repeatedly
inserted or removed from the end of an array, Octave detects it as stack
usage and attempts to use a smarter memory management strategy by
pre-allocating the array in bigger chunks.  This strategy is also applied
to cell and struct arrays.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">a = [];
while (condition)
  ...
  a(end+1) = value; # &quot;push&quot; operation
  ...
  a(end) = []; # &quot;pop&quot; operation
  ...
endwhile
</pre></div></div>

</li><li>Avoid calling <code class="code">eval</code> or <code class="code">feval</code> excessively.
Parsing input or looking up the name of a function in the symbol table are
relatively expensive operations.

<p>If you are using <code class="code">eval</code> merely as an exception handling mechanism, and not
because you need to execute some arbitrary text, use the <code class="code">try</code>
statement instead.  See <a class="xref" href="The-try-Statement.html">The try Statement</a>.
</p>
</li><li>Use <code class="code">ignore_function_time_stamp</code> when appropriate.
If you are calling lots of functions, and none of them will need to change
during your run, set the variable <code class="code">ignore_function_time_stamp</code> to
<code class="code">&quot;all&quot;</code>.  This will stop Octave from checking the time stamp of a
function file to see if it has been updated while the program is being run.
</li></ul>

</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Examples.html">Examples</a>, Previous: <a href="Memoization.html">Memoization</a>, Up: <a href="Vectorization-and-Faster-Code-Execution.html">Vectorization and Faster Code Execution</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>