Matlab R2016 runs much slower calls to .m files than R2013
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Steven Lord
am 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.
Akzeptierte Antwort
Weitere Antworten (1)
Philip Borghesani
am 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 Kommentare
Philip Borghesani
am 7 Mär. 2017
Bearbeitet: Philip Borghesani
am 7 Mär. 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.
- 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.
- 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.
- 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.
- At some point your code will be ready for the latest version of matlab and you will want to use some new feature.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!