通过引用传递MATLAB函数 [英] MATLAB function passing by reference
问题描述
我有一个带有属性的类(假设类文件的名称为inputvar
),
I have a class with properties in it (let say the name of the class file is inputvar
),
我将它用作两个不同函数的输入参数,这两个函数具有完全相同的计算,但是代码略有不同,稍后将对此进行解释.
I use it as the input argument for two different functions, which have an exactly the same calculation, but a little bit different code, which I'll explain later.
对于第一个函数(假设名称为myfun1),我这样写输入参数:
For the first function (let say the name is myfun1), I wrote the input argument like this:
f = myfun1 (inputvar)
因此,每次我想使用函数内部类中的变量时,都必须调用inputvar.var1
,inputvar.var2
等.
So every time I want to use variables from the class inside the function, I'll have to call inputvar.var1
, inputvar.var2
, and etc.
对于第二个函数(myfun2),我在输入参数中写入了该类中的每个变量,因此看起来像这样:
For the second function (myfun2), I wrote each variables from the class in the input argument, so it looks like this:
f = myfun2 (inputvar.var1, inputvar.var2, ... etc )
在函数内部,我只需要使用var1
,var2
等,而不必包含类的名称.
Inside the function, I just have to use var1
, var2
, and etc, without having to include the name of the class.
运行两个功能后,我发现myfun2
的运行速度比myfun1
快很多,大约60%(我使用tic-toc).
After running both functions, I found that myfun2
runs a lot faster than myfun1
, about 60% (I used tic-toc).
有人可以确切地向我解释为什么吗?
Can someone explain to me exactly why is that ?
推荐答案
我发现在Matlab中访问属性的速度非常慢.我尚未找到解决方法,但是可以在此处找到一些基本思路:
I found that accessing properties is very slow in Matlab. I have not found a way around it, but some basic ideas are found here: http://blogs.mathworks.com/loren/2012/03/26/considering-performance-in-object-oriented-matlab-code/
但是本文仅讨论避免可怕的,糟糕的表现.即使具有最简单的属性,其性能充其量也是中等水平.
But this article talks only about avoiding horrible, abysmal performance. Even with the simplest properties, performance is mediocre at best.
从Mathworks文章中获取示例类.我做了小的测试脚本:
Take the example class from the Mathworks article. I did small test script:
clear all
clc
n = 1e5;
%% OOP way - abysimal
result = zeros(1, n);
tic
for i = 1:n
cyl = SimpleCylinder();
cyl.R = i;
cyl.Height = 10;
result(i) = cyl.volume();
end
toc
%% OOP Vectorized - fair
clear result
tic
cyl = SimpleCylinder();
cyl.R = 1:n;
cyl.Height = 10;
result = cyl.volume();
toc
%% for loop without objects - good
result = zeros(1, n);
tic
for i = 1:n
result(i) = pi .* i.^2 .* 10;
end
toc
%% Vectorized without objects - excellent
clear result
tic
R = 1:n;
result = pi .* R.^2 .* 10;
toc
具有以下结果:
Elapsed time is 6.141445 seconds.
Elapsed time is 0.006245 seconds.
Elapsed time is 0.002116 seconds.
Elapsed time is 0.000478 seconds.
如您所见,每个媒体资源的访问速度都在降低.尝试进行向量化(一如既往),但即使是简单的for循环,对于小n而言,其性能也要优于向量化的OOP解决方案. (在我的PC上,它们在1e7时达到收支平衡)
As you can see, every property access is slowing down. Try to vectorize (as always) but even the simple for-loop outperforms the vectorized OOP solution for small n. (On my PC, they break even at 1e7)
基本信息:Matlab中的OOP太慢了!您需要为每次访问物业支付费用.
Essential message: OOP in Matlab is slow! You pay the price for every property access.
您的问题:致电时
myfun2 (inputvar.var1, inputvar.var2, ... etc )
将复制值.在该函数内,您不再需要处理类.快速访问变量.但是,如果您通过整个类,则对属性的每次访问都会很慢.您可以通过将所有属性缓存在局部变量中并使用它们来避免这种情况.
the values are copied. Within the function, you are no longer dealing with classes. Access to variables is fast. However, if you pass the whole class, every access to a property is slow. You can circumvent this by caching all properties in local variables and use these.
如果您将类修改为从handle继承,则一切都会更快一些,但差异可以忽略不计.
If you modify the class to inherit from handle everything gets a bit faster, but the difference is negligible.
这篇关于通过引用传递MATLAB函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!