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

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

6 Comments

A script has access to the base workspace, while a function has it's own workspace. That might be a reason for a speed-up. Why R2017a and R2017b would be slower than R2014b in either case is a mystery to me.
Appart from saying that you'll have to test your actual code to see on what release it will run fastest, I don't know how to really answer your question.
Scripts have access to the workspace of the function that invoked them, not to the base workspace specifically.
The images are really hard to read. I cannot decipher the relevant details reliably. Please post the code and results as text, not as screen shots. Calling "Untitled" and "Untitled" is not really clear also.
Thanks for the advice. Untitled is the name of my function / script.
Code and results of execution will update in title p.s. updated screenshots
"A script has access to the base workspace"
Only for scripts run from the command line (and a few other cases). The documentation states that "When you call a script from a function, the script uses the function workspace", which is not the base workspace.
@Stephen, I was already corrected by Walter. I misspoke because I only use scripts in the base workspace for debugging, so for all my use cases it is in fact the base workspace. But, yes, you are correct, it's not actually the base workspace necessarily, but the workspace of the calling function.

Sign in to comment.

 Accepted 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.

4 Comments

For me it remains unclear why the code execution as a script and as a function in the Matlab2014b faster than in version Matlab2017a
I'll try to file a bug report. Thanks for the advice.
p.s. as I understood the bug report is carried out through technical support. but for me technical support is not possible, because I am a student license holder
When I add timeit() calls on the multiplication, I can see that they take essentially the same time for script or function.
I saw some hints that it might have to do with the management of the variable the result is stored in, but the connection was not strong and not nearly enough to account for the timing differences.
Philip is staff, and has filed a bug report about it already.
Hi @Philip Borghesani, has there been any resolution to this issue? I am experiencing very similar performance degradation in some fairly simple scripts when using newer versions of Matlab. I have seen this on multiple workstations with varying specs. In general, 2014b ran nearly 10x faster than 2017b and 2019b. Look forward to your response.

Sign in to comment.

More Answers (1)

The Just In Time engine historically compiled functions more than it compiled scripts. This had to do with the fact that scripts were more free to "poof" variables into existence, so decisions involving variables that might be set in a script call had to be made at run time, whereas static analysis for functions could be more thorough.
In R2015b a new Execution Engine started making more assumptions about what was happening in scripts, and started declaring that some potential changes in scripts would no longer be paid attention to or would now be errors. As a result, performance of code that included scripts improved.

2 Comments

performance of code that included scripts improved
But not in the case mentioned by the OP:
2014b: 1.962136 seconds.
2017a: 8.741258 seconds.
Ah, yes, I am able to replicate the script slowdown for that code between R2014a and R2018a.

Sign in to comment.

Categories

Products

Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!