Coding Tips - Untitled
Description: Untitled

Next: Comment Tips, Previous: Style Tips, Up: Tips and Standards

### C.2 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 of

                result = [];
                for i = ever:and_ever
                  result = [ result, new_value() ];
                endfor

- Avoid calling `eval` or `feval` whenever possible, because they require Octave to parse input or look up the name of a function in the symbol table.

  If you are using `eval` as an exception handling mechanism and not because you need to execute some arbitrary text, use the `try` statement instead. See The `try` 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.