performance - MATLAB 按值传递与按对象传递

标签 performance matlab oop parameter-passing

有人可以解释一下为什么会有显着的时间差异吗?

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

class am(我知道有人可能会说没有理由为此使用类,但整个代码是更复杂和长的代码的简化,并且类是必要的):

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 将参数传递给函数的方式(通过值或对象)确实会影响性能?

最佳答案

我认为这与按值或引用将对象传递给函数没有太大关系。这只是因为在您的循环中,在 myfun2() 中,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

关于performance - MATLAB 按值传递与按对象传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17982911/

相关文章:

c# - 多线程会提高单处理器的计算速度吗

performance - 忽略lua中代码的最有效方法是什么?

javascript - 更好的搜索算法来提高性能?

计算两个 vector 之间的角度(atan2 不连续问题)

php - PHP中的观察者模式——不同事件的正确做法

Javascript、原型(prototype)对象、jQuery 和计时器

mysql - 在这种情况下,如何使 MySQL 与平面文件一样快?

arrays - 为什么 MATLAB 对结构数组赋值中的字段顺序敏感?

Matlab如何轻松循环圆形数组

C++ I/O 文件流相对于 C 语言的优势