Matlab R2016 runs much slower calls to .m files than R2013

2 views (last 30 days)
I am completely new to Matlab R2016b (previous version R2013a) and the same code runs around 10 times slower (profile checked) in the new version. The code has several calls to .m files (not functions, just scripts) and all signs that calls are the problem. If I run the complete code, without calls, there are minimal differences between them; although surprisingly 2016 is still slower (1.5 times)...frustrating.
Maybe several calls to a .m file is not the most efficient manner, but it allows me to keep the code and different versions organized...anyway, why Matlab2016b is slower?
Thanks in advance!
  2 Comments
Steven Lord
Steven Lord on 2 Feb 2017
Please post a SMALL segment of code with which you can reproduce this behavior and/or send the code to Technical Support for investigation.
Javier
Javier on 4 Feb 2017
I have wrote a simple code in order to show it. First, I Run And Time the program General.m:
H=30;
a=rand(H*86400,1);
d=zeros(H*86400,1);
for i=1:size(a,1)-H
b=max(a(i:i+H));
c=min(a(i:i+H));
d(i)=b*c;
end
Total time in Matlab R2013a: 14s. Total time in Matlab R2016b: 5.4s Congrats Matlab 2016!
However, if I split the original code in two parts General_v2.m:
H=30;
a=rand(H*86400,1);
d=zeros(H*86400,1);
for i=1:size(a,1)-H
sub_max
d(i)=b*c;
end
where sub_max.m is
b=max(a(i:i+H));
c=min(a(i:i+H));
the results are:
Total time in Matlab R2013a: 46s. Total time in Matlab R2016b: 108s!!!
2013 version runs the same code 3 times slower, but 2016 20 times slower!
I really do not understand at all! Thanks again!

Sign in to comment.

Accepted Answer

Javier
Javier on 16 Mar 2017
It seems that new Matlab version R2017a has solved this problem, see Performance section: "MATLAB has improved the performance of invoking scripts, especially when invoking a script from another script.".
I have just installed it and tested the code and now the speed between 2013 and 2016 is very similar. I just want to put this on the record in case someone has the same problem.
Thank you all guys for your time.
  1 Comment
Jan
Jan on 16 Mar 2017
I've accepted this answer, because it solves the problem. Thanks Javier for this detailed problem description.

Sign in to comment.

More Answers (1)

Philip Borghesani
Philip Borghesani on 28 Feb 2017
This answer is a bit late but the problem here is breaking code up into scripts instead of functions. Using R2016b you can create local functions inside of scripts and will find that this code runs nearly as fast as the original:
  • mycode.m:
tic
H=30;
a=rand(H*86400,1);
d=zeros(H*86400,1);
for i=1:size(a,1)-H
d(i)=sub(a,i,H);
end
toc
function prod=sub(a,i,H)
b=max(a(i:i+H));
c=min(a(i:i+H));
prod=b*c;
end
In any matlab version you can use the same code but place sub in a separate file. In the long run your code will be much more maintainable and run faster if you learn to work with functions.
  5 Comments
Javier
Javier on 7 Mar 2017
Thanks to all of you for your suggestions. During last days I have tried a solution that involves v2struct every time I call a function (pack/unpack), something like this:
S = v2struct;
S=my_function(S);
v2struct(S);
where myfunction.m is:
function S=myfunction(S)
v2struct(S);
main code
S = v2struct;
Of course this solution is much worse than the original code, calling .m files in R2016. I have to repeat this way of calling functions around 365*17280 times, so Matlab spends all the time in the v2struct function. Is there any way of doing this in a more efficient way?
I suppose that a suggestion could be to change the whole code and rename all the variable as a structure, just adding before the variable name S.variable_name . However, I have around 190 variables spread in 25 scripts (now functions) with (you can imagine) several lines of code, so therefore, and without a doubt, I would return to Matlab 2013.
Philip Borghesani
Philip Borghesani on 7 Mar 2017
Edited: Philip Borghesani on 7 Mar 2017
I for one would not want to be maintaining your code base. Rework (refactor) your code one step at at time. Structs are not the whole answer but may be part of the solution.
  1. Start on the inside: find a block of code that has a minimal set of dependencies and preferably contains at least the inner for loop and turn that into a function.
  2. If there are multiple blocks of code that use the same super set of parameters then place those in a strut but never do the pack/unpack thing, use find and replace to update all uses. You will find that even in R2013 your code will run faster and be easier to maintain.
  3. As a stopgap measure you might try using some globals. I would not normally recommend this but in your case globals are better then one colossal workspace and all scripts.
  4. At some point your code will be ready for the latest version of matlab and you will want to use some new feature.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!