To a very good first approximation, the goal in vectorization is to write code that avoids loops and uses whole-array operations. As a trivial example, consider
for i = 1:n for j = 1:m c(i,j) = a(i,j) + b(i,j); endfor endfor
compared to the much simpler
c = a + b;
This isn’t merely easier to write; it is also internally much easier to optimize. Octave delegates this operation to an underlying implementation which, among other optimizations, may use special vector hardware instructions or could conceivably even perform the additions in parallel. In general, if the code is vectorized, the underlying implementation has more freedom about the assumptions it can make in order to achieve faster execution.
This is especially important for loops with "cheap" bodies. Often it suffices to vectorize just the innermost loop to get acceptable performance. A general rule of thumb is that the "order" of the vectorized body should be greater or equal to the "order" of the enclosing loop.
As a less trivial example, instead of
for i = 1:n-1 a(i) = b(i+1) - b(i); endfor
write
a = b(2:n) - b(1:n-1);
This shows an important general concept about using arrays for indexing instead of looping over an index variable. See Index Expressions. Also use boolean indexing generously. If a condition needs to be tested, this condition can also be written as a boolean index. For instance, instead of
for i = 1:n if (a(i) > 5) a(i) -= 20 endif endfor
write
a(a>5) -= 20;
which exploits the fact that a > 5
produces a boolean index.
Use elementwise vector operators whenever possible to avoid looping (operators like .*
and .^
). See Arithmetic Ops. For simple inline functions, the vectorize
function can do this automatically.
Create a vectorized version of the inline function fun by replacing all occurrences of *
, /
, etc., with .*
, ./
, etc.
This may be useful, for example, when using inline functions with numerical integration or optimization where a vector-valued function is expected.
fcn = vectorize (inline ("x^2 - 1")) ⇒ fcn = f(x) = x.^2 - 1 quadv (fcn, 0, 3) ⇒ 6
Also exploit broadcasting in these elementwise operators both to avoid looping and unnecessary intermediate memory allocations. See Broadcasting.
Use built-in and library functions if possible. Built-in and compiled functions are very fast. Even with an m-file library function, chances are good that it is already optimized, or will be optimized more in a future release.
For instance, even better than
a = b(2:n) - b(1:n-1);
is
a = diff (b);
Most Octave functions are written with vector and array arguments in mind. If you find yourself writing a loop with a very simple operation, chances are that such a function already exists. The following functions occur frequently in vectorized code:
© 1996–2018 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/interpreter/Basic-Vectorization.html