MATLAB: Different speed of execution of the same code in different versions Matlab. 2014b and 2017a

2014b2017acalculation perfomancejitMATLAB

Hello. The question is this. Why are the same code executed at different speeds in different versions of matlab? Versions 2014b and 2017a.
Below are screenshots with the difference in code execution in different versions.
I noticed one strange thing. If I write code without declaring a function, then the difference in execution between versions is huge.
If the code is described as a function, then speed is greatly increased
code without declaring a function:
clear
b = rand(1000000,10);
i=1;
tic
while i <= 1000
res = b(:,1).*b(:,10);
i = i+1;
end
toc
result Matlab 2014b :
Elapsed time is 1.962136 seconds.
result Matlab 2017a :
Elapsed time is 8.741258 seconds.
code with function:
function f=Untitled
clear
b = rand(1000000,10);
i=1;
tic
while i <= 1000
res = b(:,1).*b(:,10);
i = i+1;
end
toc
end
result Matlab 2014b :
Elapsed time is 1.757147 seconds.
result Matlab 2017a :
Elapsed time is 1.885382 seconds.
p.s. Untitled is the name of the script / function

Best Answer

This feels like a bug to me and I filed a bug report. Walter is correct that coding this as a function is expected to produce faster code but there is no reason for this big a performance difference.
If you can't easily convert your code to a function I suggest posting more information about the problem you are trying to solve with actual code, or creating a support call for the best solutions to this performance issue.
Related Question