2013-07-31 39 views
2

有人可以解释为什么有显着的时间差异?MATLAB通过值vs对象

function [] = maincalc() 

ak=am(); 

t1=tic; 
[out] = myfun (ak.aa, ak.b, ak.c, ak.d, ak.e, ak.f, ak.g, ak.h); 
to1end=toc(t1) 

t2=tic; 
[out2] = myfun2 (ak); 
to2end=toc(t2) 

结果:

to1end = 
    0.047520231560659 
to2end = 
    12.490895284055467 

类时(我知道有人会说,没有理由使用类此,而是整个代码是一个更为复杂和长期的简化代码,和类是必要的):

classdef am 
    properties 
     aa = 1; 
     b = 2; 
     c = 3; 
     d = 4; 
     e = 2.3; 
     f = 4.2; 
     g = 5.09; 
     h = 12.3; 
    end 
end 

功能myfun:

function [out] = myfun (aa, b, c, d, e, f, g, h) 
n = 500000; 
i = 0; j = 0; k = 0; l = 0; 
for s = 1:n 
    i = aa/b + j*k - i; 
    j = c/d^0.5 - j/i; 
    k = e*f + aa/3 - k/8; 
    l = g + exp (h) + l^-1; 
end 
out.i = i; 
out.j = j; 
out.k = k; 
out.l = l; 

功能myfun2:

function [out] = myfun2 (ak) 
n = 500000; 
i = 0; j = 0; k = 0; l = 0; 
for s = 1:n 
    i = ak.aa/ak.b + j*k - i; 
    j = ak.c/ak.d^0.5 - j/i; 
    k = ak.e*ak.f + ak.aa/3 - k/8; 
    l = ak.g + exp (ak.h) + l^-1; 
end 
out.i = i; 
out.j = j; 
out.k = k; 
out.l = l; 

我看过有人的地方解释有关MATLAB的副本上写的,但并没有真正适用于这里,因为没有对类中的任何成员所做的更改。

============================================== ==================================== 最近在2013年8月2日新增了这条线的详细信息Marcin回答说,它与MATLAB传递参数给函数的方式没有多大关系(顺便提一下,很棒的发现!),但我认为它仍然与它有关。我做了另一个代码,这一次所有三种方法需要访问该类多次:

function [] = maincalc3() 

inputvar=inputclass(); 

to1end = 0; 
to2end = 0; 
to3end = 0; 
j = 100; 

for i = 1:j; 
    t1=tic; 
    [out] = func1 (inputvar); 
    to1end=toc(t1) + to1end; 

    t2=tic; 
    [out2] = func2 (inputvar.s); 
    to2end=toc(t2) + to2end; 

    t3=tic; 
    [out3] = func3 (inputvar); 
    to3end=toc(t3) + to3end; 
end 

...................... ........

classdef inputclass 
    properties 
     s = 1; 
    end 
end 

...............................

function f = func1 (inputvar) 
    f = inputvar.s; 
end 

...............................

function f = func2 (s) 
    f = s; 
end 

...............................

function [f] = func3 (inputvar) 
    s=inputvar.s; 
    f = s; 
end 

和结果:

to1end = 
    0.002419525505078 
to2end = 
    0.001517134538850 
to3end = 
    0.002353777529397 

func1()func3()需要大约相同的时间,但func2需要约60%的时间。这是不是意味着MATLAB将参数传递给函数的方式 - 通过值或对象 - 确实会影响性能?

+1

Matlab中的OOP很慢。与本地变量相比,属性访问可能花费更多时间。添加方法get/set时更糟糕。您可以查看此网页:http://stackoverflow.com/questions/1693429/is-matlab-oop-slow-or-am-i-doing-something-wrong – Yuan

+0

是啊,我同意,这是最好的,如果能避免OOP 。但我现在的任务是弄清楚为什么需要更多时间。 – saiful

回答

4

我认为它与通过值或引用将对象传递给函数没有多大关系。其原因很简单,因为在你的循环中,matlab需要多次访问对象及其字段。而已。

例如,如果你让叫myfun3()第三功能如下:

function [out] = myfun3 (ak) 
n = 500000; 
i = 0; j = 0; k = 0; l = 0; 

% FOLLOWING LINE IS NEW <----- 
% REMOVE object refencese from within the loop and create local variables 
% to use in the loop, instead of referencing object properties all the time. 
aa = ak.aa; b = ak.b; c=ak.c; d=ak.d; e=ak.e; f=ak.f; g=ak.g; h=ak.h; 

for s = 1:n 
    i = aa/b + j*k - i; 
    j = c/d^0.5 - j/i; 
    k = e*f + aa/3 - k/8; 
    l = g + exp (h) + l^-1; 
end 
out.i = i; 
out.j = j; 
out.k = k; 
out.l = l; 

这个函数的执行甚至略快于myfun1()。在我的电脑上的数据是:

to1end = 

    0.0533 


to2end = 

    23.9410 


to3end = 

    0.0526 % RESULT for myfun3() function 
+0

很棒的发现,Marcin!我为这个问题添加了新的细节,我希望你也可以看看。 – saiful