Matlab memory optimization regarding output arguments
2 次查看(过去 30 天)
显示 更早的评论
Suppose we have the following code in functie.m file:
"
function functie()
x = [9];
x = functieLocala(x);
disp(x);
end
function y = functieLocala(x)
if length(x) > 10
y = [x 1];
else
y = x;
end
end
"
The question is will Matlab make the output argument of functieLocala y be a copy of the input argument x, or y will share the data of x? On one hand, function called functie tells that x doesn't need to be preserved, but I do not know how functieLocala behaves in terms of copying or sharing the value(s) of the input argument x to the output argument y.
0 个评论
采纳的回答
lazymatlab
2023-12-14
编辑:lazymatlab
2023-12-14
It depends.
If the else block runs (y = x) in functieLocala, y shares x because it is just another name of same matrix. If length(x) > 10, then y is not same as x. So the values of x are copied to y and they does not share anymore.
You can test it using memory and making a sufficient large array.
clear
memory
x = functie();
memory
function x = functie()
x = rand(10000);
x(1,1) = 0; % change here and see what happens
memory
x = functieLocala(x);
memory
end
function y = functieLocala(x)
if x(1,1) ~= 0
y = [x rand(10000, 1)];
else
y = x;
end
memory
end
3 个评论
lazymatlab
2023-12-15
It says,
"When you assign an array to a second variable (for instance, when you execute B = A), MATLAB does not allocate new memory right away. Instead, it creates a copy of the array reference. As long as you do not modify the contents of the memory block being referenced by A and B, there is no need to store more than one copy of data. However, if you modify any elements of the memory block using either A or B, MATLAB allocates new memory, copies the data into it, and then modifies the created copy."
Bruno Luong
2023-12-15
编辑:Bruno Luong
2023-12-15
No worry, the copy on write I know for decades.
But my point is MATLAB can even go further in the optimization and might do NOT do copy-on-write if the function prototype is
x = functieLocala(x)
for both if branch, and the local x is on functie does not share with anything. This is call inplace assigment and the Loren blog (article 2007) recommends to do that. She uses Windows memory to show the behavior NOT MATLAB memory since it is not observable by this MATLAB command.
Recent MATLAB does even further memory optimization from few of the tests, the behavior is not very clear to me, but it looks like it can keep track of variable read and write of user intention, and do something smart on that.
更多回答(1 个)
Bruno Luong
2023-12-14
You better have your funtion codded like this
function functie()
x = 9; % Remove the braket
x = functieLocala(x);
disp(x);
end
function x = functieLocala(x) % make inplace otput when it can
if size(x,2) > 10 % use size rather than length
x = [x 1];
% remove the else branch
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!