octave: Miscellaneous Techniques

 
 19.6 Miscellaneous Techniques
 =============================
 
 Here are some other ways of improving the execution speed of Octave
 programs.
 
    • 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.
 
    • Be aware of lazy copies (copy-on-write).  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:
 
           a = zeros (1000); # create a 1000x1000 matrix
           b = a; # no copying done here
           b(1) = 1; # copying done here
 
      Lazy copying applies to whole Octave objects such as matrices,
      cells, struct, and also individual cell or struct elements (not
      array elements).
 
      Additionally, index expressions also use lazy copying when Octave
      can determine that the indexed portion is contiguous in memory.
      For example:
 
           a = zeros (1000); # create a 1000x1000 matrix
           b = a(:,10:100);  # no copying done here
           b = a(10:100,:);  # copying done here
 
      This applies to arrays (matrices), cell arrays, and structs indexed
      using ‘()’.  Index expressions generating comma-separated lists can
      also benefit from shallow copying in some cases.  In particular,
      when A is a struct array, expressions like ‘{a.x}, {a(:,2).x}’ will
      use lazy copying, so that data can be shared between a struct array
      and a cell array.
 
      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 "permanent"
      location, such as a named variable or cell or struct element, and
      possibly economizes them.  For example:
 
           a = zeros (1000); # create a 1000x1000 matrix
           b = a(:,10:100);  # lazy slice
           a = []; # the original "a" array is still allocated
           c{1} = b; # b is reallocated at this point
 
    • 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.
 
    • 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
             ridx = ...
             cidx = ...
             result(ridx, cidx) = new_value ();
           endfor
 
      instead of
 
           result = [];
           for i = ever:and_ever
             result = [ result, new_value() ];
           endfor
 
      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.
 
           a = [];
           while (condition)
             ...
             a(end+1) = value; # "push" operation
             ...
             a(end) = []; # "pop" operation
             ...
           endwhile
 
    • Avoid calling ‘eval’ or ‘feval’ excessively.  Parsing input or
      looking up the name of a function in the symbol table are
      relatively expensive operations.
 
      If you are using ‘eval’ merely as an exception handling mechanism,
      and not because you need to execute some arbitrary text, use the
      ‘try’ statement instead.  SeeThe try Statement.
 
    • Use ‘ignore_function_time_stamp’ when appropriate.  If you are
      calling lots of functions, and none of them will need to change
      during your run, set the variable ‘ignore_function_time_stamp’ to
      "all".  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.