MATLAB: Why a is function call from within a function much much slower than not doing it in a function.

MATLABperformancerun time

here is a simple code that shows it:
function c=myfunc(a,b)
c=a*b;
function c=main_temp()
a(1)=5;
a(2)=8;
r=1e7;
tic
for i=1:r
c=myfunc(a(1),a(2));
end
toc
tic
for i=1:r
c=a(1)*a(2);
end
toc
end
here is the timing using tic-toc
Elapsed time is 0.327171 seconds.
Elapsed time is 0.009264 seconds.
it's about 40 times slower (for no apparent reason except function overheads). and it gets much worse when using classes (about 15 times slower then the function call).
i searched extensively around the web and read every performance post and guidances related post.
also, this is a very very simple code that shows a core problem.

Best Answer

This is not a problem, but it shows that Matlab's JIT acceleration works powerful. With the inlined code it seems to recognize, that c is overwritten by the same values repeatedly. Maybe the JIT exploits, that the same indices are used, such that it does not access the array dynamically, but re-uses the values directly.
All this cannot happen, if the code to be evaluated is stored in an extra function. The smaller the function is, the less possibilities does the JIT have to accelerate the code.
Note that the JIT is not documented and subject to frequent changes. Therefore the idea, why exactly it works more powerful in the inlined code is an educated guess only.
Trying to improve such tiny artificial parts of the code is called "pre-mature optimization". This is a typical anti-pattern in programming. Prefer to write clean and working code and let the profiler find the bottlenecks at the end. In a real-world program you can observe the advantage of inlined code also, but the effect is usually smaller. Most of all, try not to create too tiny subfunctions.
Matlab's object oriented classes are known to be not so fast.