help-octave
[Top][All Lists]
Advanced

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

Re: avoiding copying arguments by value when calling a function


From: Carnë Draug
Subject: Re: avoiding copying arguments by value when calling a function
Date: Mon, 20 Aug 2012 15:15:32 +0100

On 20 August 2012 14:55, Sergei Steshenko <address@hidden> wrote:
> Hello,
>
> if I understand correctly, if I write a function like this:
>
> function result = foo(bar)
>   # function body goes here
> endfunction
>
> and if I call the function this way
>
> result = foo(bar);
>
> , the 'bar' argument is copied by value each time 'foo' is called.
>
>
> I was thinking of something like this:
>
>
> bar_func = @() bar; # Octave accepts this
>
> function result = foo(bar_func)
>   # function body goes here
>   # whenever I need 'bar' I use bar_func() instead
> endfunction
>
> - I tried this and it works, i.e. produces the expected result.
>
> If again I understand correctly, passing 'bar_func' is passing a function 
> handle, which for large enough 'bar' data can hopefully yield better 
> performance.
>
> Are my understandings correct ? I.e. can anonymous functions be used as data 
> wrappers in order to avoid physical copying of data ?

I started some time ago a page on the wiki about performance
http://wiki.octave.org/Performance and no, Octave does not use Copy by
value. It's more like lazy copy. Seems the manual
http://www.gnu.org/software/octave/doc/interpreter/Call-by-Value.html
is incorrect.

I'm aware the wiki page it's quite incomplete and what was there I had
discovered myself by creating a giant matrix, copying it around and
measuring the time it took. Some time ago someone also asked about
using global variables hence why there's a section named Global
variables but I never tried that. If you investigate its use, please
add it to the wiki.

Carnë


reply via email to

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