MATLAB中形式为x = f(x)的函数调用的内存分配 [英] Memory-Allocation for function calls of form x = f(x) in MATLAB
问题描述
在我的代码中,有很多地方可以调用
In my code, I have lots of places where I invoke functions of the form
X = f(X)
和X可以是一个相当大的矩阵.在我的特殊情况下,我通常会打电话给
and X can be a rather large matrix. In my special case, I have mostly calls like
X = feval(somefunc, X)
或
X = obj.myfunc(X)
如果每次调用该函数都为X分配新的空间,那将是不好的.MATLAB是否足够智能以应对此类函数调用?有办法说吗?
It would be bad if, every time the function is called, there is new space allocated for X. Is MATLAB smart enough to deal with such function calls? Is there a way to tell?
这个问题的答案将有助于设计决策.我喜欢以面向对象的方式进行编程,如果MATLAB不够智能,那么为我在类中添加X的另一个成员可能会有所回报,尽管我宁愿不这样做.
The answer to this question helps would help very much with a design descision. I like to program in object oriented style and if MATLAB is not smart enough for this, it might pay off for me to add another member for X in the class, although I would rather not do this otherwise.
推荐答案
在MATLAB中调用函数时,是否由输入参数构成副本取决于函数内部发生的情况.
Whether a copy is made of the input arguments or not when calling a function in MATLAB depends upon what happens inside of the function.
MATLAB使用的系统称为
MATLAB uses a system referred to as copy-on-write. This means that if you pass a large variable to a function as an input, as long as you do not modify the variable within that function, the variable will not be copied into the workspace of the function and the function will instead read the data from it's current location in memory.
function Y = func(X)
Y = X + 1;
end
如果正在修改函数中的变量,那么将创建输入变量的副本并将其放置在函数的本地工作空间中.
If you are modifying the variable within the function, then a copy of the input variable is made and placed into the local workspace of the function.
function X = func(X)
X = X + 1;
end
有关确定是否复制数据的一种简单方法是使用未公开的format debug
模式,该模式将向您显示给定变量的数据在内存中的存储位置.
An easy way to determine if a copy of the data is made or not is to use the undocumented format debug
mode which will show you where the data for a given variable is stored in memory.
format debug
%// Create a variable a and show where debug info
a = [1,2]
%// Structure address = 141f567f0
%// m = 1
%// n = 2
%// pr = 7f9540b85e20
%// pi = 0
%// 1 2
%// Assign b = a but don't modify
b = a
%// Structure address = 141f567f0
%// m = 1
%// n = 2
%// pr = 7f9540b85e20 <= POINTER TO DATA REMAINED UNCHANGED
%// pi = 0
%// 1 2
%// Modify (Will create a new copy)
b = b + 1
%// Structure address = 141f55b40
%// m = 1
%// n = 2
%// pr = 7f953bcf1a20 <= POINTER TO DATA CHANGED (COPY)
%// pi = 0
%// 2 3
如果您愿意,可以使用我创建的这个小的匿名函数来检查任何特定变量的内存位置.
If you prefer you can use this little anonymous function I've created to inspect the memory location of any particular variable.
memoryLocation = @(x)regexp(evalc('disp(x)'), '(?<=pr\s*=\s*)[a-z0-9]*', 'match')
a = [1,2];
memoryLocation(a)
%// 7f9540b85e20
b = a;
memoryLocation(b)
%// 7f9540b85e20
b = b + 1;
memoryLocation(b)
%// 7f953bcf1a20
作为一个附带说明,我建议您在整个代码中不要使用feval
,而应直接使用函数名称.
As a bit of a side-note, I would recommend against using feval
throughout your code and instead just use the function names directly.
这篇关于MATLAB中形式为x = f(x)的函数调用的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!