Why did Matlab performance massively decrease for basic arithmetic operations from Matlab R2014b to 2015 and 2017 version?

3 Ansichten (letzte 30 Tage)
I'm supposed to program a tool to run on R2015 but only have R2014b and R2017a available on the computer I am using. I thought using R2014b would be safer bet compatibility wise. So far so good. Everything seemed to be working fine. The problem is the code needs ~4s when running in R2014b but several minutes in R2015 and R2017 (using the same computer). The part of the code that takes a lot longer in the R2017 version (and I assume it is the same part in R2015) is calculating some of the variables of a system of linear equations with ~30 variables in total. To make it faster I didn't want to use a numeric solver but found the symbolic solutions. Problematic: the solution for 1 variable is >2 Million figures long even when using variable names with only one character. In a first step to make the code more manageable I found repeating patterns and replaced them. So the code looks something like:
Replace(:,1)= -D.*R.^2.*U.*n.*o.*p.*v+D.*S.^2.*U.*n.*o.*p.*w+R.^2.*U.*W.*n.*o.*p.*v %...
Replace(:,2)= M.*R.*W.*d.*n.*o.*p-.*M.*R.*n.*o.*p.*w+D.*M.*S.*n.*o.*p.*v %...
Result= Replace(:,1) .*V.*d.*n.*o-Replace(:,1).*O.^2.*S.*d.*o.*p.*w+ Replace(:,2) %...
A-Z & a-z being column vectors with double precision (double is necessary since some of the intermittend solutions get >3.4*10^38).
Does anyone have an idea why the code is running so much faster in R2014 and how I can achieve the same speed in R2015? If need be I’ll try to export the portion as C-code but I’m not too sure if that will resolve the issue(and it would be preferred to only use “direct” Matlab code).
Thanks for your help.
  11 Kommentare
Desoxyribose
Desoxyribose am 21 Apr. 2018
Bearbeitet: Desoxyribose am 21 Apr. 2018
Philip, the code is in a function and does not contain an eval().
Anyways I shamefully have to admit: The first suggestions from John (which I didn't understand at that time) and then explained by Christine of simply using "backslash" did proof to be faster by a magnitude than my code even in R2014. When I tried it before my "symbolic solve" approach I must have had an (or some) errors) in the original equations which is why I stubbornly proclaimed it to be slower (it was some while ago so I don't remember everything 100%).
Anyways: Thank you everybody. You helped me find a great solution to my problem and I have learned several things along the way (one of it being that a predecessor doesn't always have to be right/taken the best approach). Since only Walters comments is listed as "Answer" I'll accept his answer since I think the hint with the compiler change from 2014 to 2015 might be useful for others to see.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Apr. 2018
Bearbeitet: Walter Roberson am 20 Apr. 2018

"In a first step to make the code more manageable I found repeating patterns and replaced them."

If you use matlabFunction() with the 'file' option naming a file, then by default 'Optimize' is true for that case, and MATLAB will spend time looking for repeated computations and making appropriate temporary variables.

The resulting code is not typically all that easy to read, but it does tend to be about as efficient as you could get. (Well, other than the fact that it does not always optimize calculations of .^ to higher integer powers as best possible.)

The drawback of this is that it can take a fair bit of time. I have seen it take over an hour for longer expressions. For a calculation such as you describe it would not surprise me if it would take over an hour.

The revised calculation is equivalent algebraically, but not numerically. Numerically,

A + B*C + D

is not the same as

 A + D + B*C

However, when you are using symbolic expressions, the order that it uses internally is based upon some arbitrary sorting rules that are difficult to work out, so the order is already numerically suspect even if you do not turn on optimization.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by