performance - MATLAB passing by values vs by objects -


can explain why there significant time difference?

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) 

the result:

to1end =    0.047520231560659 to2end =     12.490895284055467 

class ( know might there no reason use class this, whole code simplification of more complex , long code, , class necessary):

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

function myfun:

function [out] = myfun (aa, b, c, d, e, f, g, h) n = 500000; = 0; j = 0; k = 0; l = 0; s = 1:n     = 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; 

function myfun2:

function [out] = myfun2 ( ak ) n = 500000; = 0; j = 0; k = 0; l = 0; s = 1:n     = 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; 

i have read somewhere explaining matlab's copy-on-write, doesn't apply here since there no changes made member of class.

================================================================================== details below line added on 8/2/2013

marcin responded has not way matlab passes parameters functions (great discovery way!), think still has it. i've made code, , time 3 approaches need access class multiple times:

function [] = maincalc3 ()  inputvar=inputclass();  to1end = 0; to2end = 0; to3end = 0; j = 100;  = 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 

and result:

to1end =    0.002419525505078 to2end =    0.001517134538850 to3end =    0.002353777529397 

func1() , func3() takes same time, func2 takes 60% less time. doesn't mean way matlab pass parameters functions - values or objects - affect performance?

i think has not passing object value or reference function. because in loop, in myfun2() matlab needs access object , fields multiple times. that's it.

for example, if u make third function called myfun3() follows:

function [out] = myfun3 (ak) n = 500000; = 0; j = 0; k = 0; l = 0;  % following line new <----- % remove object refencese within loop , create local variables % use in loop, instead of referencing object properties 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;  s = 1:n     = 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; 

the execution of function faster myfun1(). on pc resutls are:

to1end =      0.0533   to2end =     23.9410   to3end =      0.0526 % result myfun3() function 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -