~gabriel1984sibiu/octave/octave

« back to all changes in this revision

Viewing changes to doc/interpreter/octave.html/Miscellaneous-Techniques.html

  • Committer: Grevutiu Gabriel
  • Date: 2014-01-02 13:05:54 UTC
  • Revision ID: gabriel1984sibiu@gmail.com-20140102130554-3r7ivdjln1ni6kcg
New version (3.8.0) from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
2
<html>
 
3
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
 
4
<head>
 
5
<title>GNU Octave: Miscellaneous Techniques</title>
 
6
 
 
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">
 
20
<!--
 
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}
 
45
-->
 
46
</style>
 
47
 
 
48
 
 
49
</head>
 
50
 
 
51
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
 
52
<a name="Miscellaneous-Techniques"></a>
 
53
<div class="header">
 
54
<p>
 
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> &nbsp; [<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>
 
56
</div>
 
57
<hr>
 
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>
 
63
 
 
64
<p>Here are some other ways of improving the execution speed of Octave
 
65
programs.
 
66
</p>
 
67
<ul>
 
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.
 
73
 
 
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:
 
81
 
 
82
<div class="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
 
86
</pre></div>
 
87
 
 
88
<p>Lazy copying applies to whole Octave objects such as matrices, cells,
 
89
struct, and also individual cell or struct elements (not array
 
90
elements).
 
91
</p>
 
92
<p>Additionally, index expressions also use lazy copying when Octave can
 
93
determine that the indexed portion is contiguous in memory.  For example:
 
94
</p>
 
95
<div class="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
 
99
</pre></div>
 
100
 
 
101
<p>This applies to arrays (matrices), cell arrays, and structs indexed
 
102
using &lsquo;<samp>()</samp>&rsquo;.  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.
 
106
</p>
 
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 &quot;permanent&quot; location, such as a named variable or
 
113
cell or struct element, and possibly economizes them.  For example:
 
114
</p>
 
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 &quot;a&quot; array is still allocated
 
119
c{1} = b; # b is reallocated at this point
 
120
</pre></div>
 
121
 
 
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.
 
126
 
 
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
 
130
 
 
131
<div class="example">
 
132
<pre class="example">result = zeros (big_n, big_m)
 
133
for i = over:and_over
 
134
  ridx = &hellip;
 
135
  cidx = &hellip;
 
136
  result(ridx, cidx) = new_value ();
 
137
endfor
 
138
</pre></div>
 
139
 
 
140
<p>instead of
 
141
</p>
 
142
<div class="example">
 
143
<pre class="example">result = [];
 
144
for i = ever:and_ever
 
145
  result = [ result, new_value() ];
 
146
endfor
 
147
</pre></div>
 
148
 
 
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.
 
155
</p>
 
156
<div class="example">
 
157
<pre class="example">a = [];
 
158
while (condition)
 
159
  &hellip;
 
160
  a(end+1) = value; # &quot;push&quot; operation
 
161
  &hellip;
 
162
  a(end) = []; # &quot;pop&quot; operation
 
163
  &hellip;
 
164
endwhile
 
165
</pre></div>
 
166
 
 
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.
 
170
 
 
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>.
 
174
</p>
 
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>&quot;all&quot;</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.
 
180
</li></ul>
 
181
 
 
182
<hr>
 
183
<div class="header">
 
184
<p>
 
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> &nbsp; [<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>
 
186
</div>
 
187
 
 
188
 
 
189
 
 
190
</body>
 
191
</html>