MATLAB offers an optimization that can prevent a copy of a varible that is passed to a function from being created if the function returns the same variable. This in known as an "in-place" function call and can be helpful when working with large data sets.
My issue is that my large data set is part of a structure and this seems to negate the in-place optimization.
I have the following code:
function inplaceTest x = randn(2^24, 1); tic; x = f(x); toc; data.x = x; tic; data = fData(data); toc;endfunction x = f(x) x(50) = 1;endfunction data = fData(data) data.x(50) = 1;end
When I execute 'inplaceTest' I get the following:
>> inplaceTest Elapsed time is 0.001587 seconds. Elapsed time is 0.160606 seconds. >>
The call to the function 'fdata' takes longer to execute than the call to the function 'f' because a copy of the input data is being created.
Best Answer