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: Jordi Gutiérrez Hermoso
Subject: Re: avoiding copying arguments by value when calling a function
Date: Mon, 20 Aug 2012 10:07:12 -0400

On 20 August 2012 09: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.

Not quite. Octave uses COWs:

    http://en.wikipedia.org/wiki/Copy-on-write

> 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.

This indirection won't get you around COW semantics.

If you really want to get around COW semantics, you can write oct
files that directly manipulate the data in C++. It's discouraged to do
this, because it produces semantics that are different from how
anything else in Octave works.

There is experimental work in the dev sources to introduce handle
classes via Matlab's classdef mechanism, which are the only way to get
reference semantics in Matlab.

- Jordi G. H.


reply via email to

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