MATLAB Magical covert behavior

I experience very strange temporary behavior from a function that I wrote. If I close my function in another empty container function, it will get 3 times acceleration.

>> tic; Foo (arg); Toc

Elapsed time: ~ 140 seconds

>> crosses; bar (arg); Toc

time has passed: ~ 35 seconds

Here the kicker is the definition of bar ():

define bar (args)

Foo (arg)

end

Is there any optimization that runs in MATLAB for nested function calls? Should I add a dummy function for every function I write?

+3
source share
4 answers

JIT , . , "tic; foo (args), toc" foo MATLAB. , "tic; bar (args); toc", bar , JIT foo() .

, . MATLAB JIT ; , , The MathWorks. , , , - : http://blogs.mathworks.com/loren/2006/05/10/memory-management-for-functions-and-variables/#comment-207

+3

foo ? , . , , MATLAB , .

function barfoo    

    for i = 1:Inf
    end    

end

,

function foobar        
    barfoo();
end
+1

, , , , - , , (, - ), , ( foo):

tic; foo(args); toc;  % First call of foo
tic; bar(args); toc;  % Second call of foo inside bar
tic; foo(args); toc;  % Third call of foo
+1

. .

, . " Matlab ?" .

clear all
profile on -timer real
foo(args);
profile report
%read the report and save a screencap
clear all
profile clear
bar(args);
profile report

. .

, . . foo() "ans", . bar(), ans , , bar() . , foo() evalin()/assignin() , . bar() .

, bar.m, foo() , , . "foo" .

, "args", foo () s.

, foo() , , , - .

, . , - , , . bar() . , . .

+1

Source: https://habr.com/ru/post/1715734/


All Articles