help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

efficient function parameter copying?


From: John W. Eaton
Subject: efficient function parameter copying?
Date: Fri, 10 Sep 1999 09:48:11 -0500 (CDT)

On 10-Sep-1999, Michael Pronath <address@hidden> wrote:

| 
| If I declare a function in octave
| 
|    function v = f(v)
|       ...
|    endfunction
| 
| and then call it by
| 
|    ...
|    X = f(X);
|    ...
| 
| the octave interpreter could exploit that the variable X need not
| be copied into v, but could be changed directly.
| I'm using a construct like the one for iterations, i.e. calling
| f quite often, with X being a matrix.  Using global variables could
| do it as well, but are not as beautiful.  So my question is, 
| does the octave interpreter detect this case and optimize parameter
| copying, or is the matrix X copied twice unnecessarily here?

Octave values are implemented using reference-counted objects.  The
data is not duplicated unless it is necessary.  So, for something like

  function y = f (x)
    y = x;
  endfunction

  w = f (z);

only one copy of z should actually exist (but the reference count will
be incremented and decremented as necessary).

For something like

  function y = f (x)
    y = x;
    y(some_index) = some_value;  # or some other way of modifying y
  endfunction

however, a copy of x will be made when y is modified.

jwe



---------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.  To ensure
that development continues, see www.che.wisc.edu/octave/giftform.html
Instructions for unsubscribing: www.che.wisc.edu/octave/archive.html
---------------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]