GNU Octave Manual Version 3 by John W. Eaton, David Bateman, Søren Hauberg Paperback (6"x9"), 568 pages ISBN 095461206X RRP £24.95 ($39.95) |
33.8 Tips for Making Code Run Faster.
Here are some ways of improving the execution speed of Octave programs.
- Avoid looping wherever possible.
- Use iteration rather than recursion whenever possible. Function calls are slow in Octave.
-
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
result = zeros (big_n, big_m) for i = over:and_over r1 = ... r2 = ... result (r1, r2) = new_value (); endfor
instead ofresult = []; for i = ever:and_ever result = [ result, new_value() ]; endfor
-
Avoid calling
eval
orfeval
whenever possible, because they require Octave to parse input or look up the name of a function in the symbol table. If you are usingeval
as an exception handling mechanism and not because you need to execute some arbitrary text, use thetry
statement instead. See section 10.9 Thetry
Statement. -
If you are calling lots of functions but none of them will need to
change during your run, set the variable
ignore_function_time_stamp
to"all"
so that Octave doesn't waste a lot of time checking to see if you have updated your function files.
ISBN 095461206X | GNU Octave Manual Version 3 | See the print edition |