How to speed up my while loops

11 Ansichten (letzte 30 Tage)
Felix Lauwaert
Felix Lauwaert am 1 Aug. 2015
Bearbeitet: Cedric am 2 Aug. 2015
Hello,
I have been looking for ways to speed up my code because sometimes I spend some hours at running once MATLAB. I've read: http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/ But it's all about "for" loops (spectacular post, BTW). The problem is that I mainly have "while" loops so I don't get how could I vectorize them. The main problem is that I call another function I've made inside my "while" loop, and that function has another "while" which calls another function with another "while".
I would appreciate any tip. Here you have part of one of my functions, if you want an example:
PSspan = [t0 t0+increment];
[resu] = nInt(ab,PSspan,hmax,tol);
[resu] = ham(resu);
[resuPol] = convPol(resu);
while iPS<nPS && loop<loopLimit
loop=loop+1; if mod(loop,250)==0; display(loop); end
ab=resu(2:5,end);
t0=resu(1,end); PSspan=[t0 t0+increment];
[resu] = nInt(ab,PSspan,hmax,tol);
[resu] = ham(resu);
[resuPol] = convPol(resu);
%Comprovació de si s'ha arribat a PS per thetaP
switch tipusCoord
case 'Cartesianes';
p=resu(variablePS,1);
q=resu(variablePS,end);
ctrl=resu(vControl,1);
case 'Polars';
p=resuPol(variablePS,1);
q=resuPol(variablePS,end);
ctrl=resuPol(vControl,1);
end
if p*q<0 && ctrl>0
%Poincaré section
iPS=iPS+1; display(iPS); loop=0;
[ coordCart, coordPol ] = BiseccioMod( resu , hmax, tipusCoord, variablePS );
resuPS(:,iPS)=coordCart; resuPSPol(:,iPS)=coordPol;
end
end
  4 Kommentare
Jon
Jon am 2 Aug. 2015
My gut tells me that it would take you much longer to vectorize this (if even possible) than it would take just to run all the test cases you want.
Once you've preallocated and vectorized your code as much as possible, look into parfor if you really need to save time. You can likely parfor the different intitial conditions.
Felix Lauwaert
Felix Lauwaert am 2 Aug. 2015
You're probably right. I'll have a better look at parfor, although I've allready used it a little. Thanks

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 1 Aug. 2015
Bearbeitet: Jan am 1 Aug. 2015
Why do you assume, that the while loop needs a remarkable amount of time in your code? The profiler will reveal, which lines or subfunctions require the most time. When the loop itself takes a few percent of the processing time only, an optimization is not efficient.
Matlab's JIT acceleration is impeded, if lines contain more than one command. Therefore and to improve the readability, use one command per line only.
  5 Kommentare
Cedric
Cedric am 2 Aug. 2015
Bearbeitet: Cedric am 2 Aug. 2015
Difficult to know without testing ourselves, but it is a good setup for learning to use the profiler and to analyze reports. If you cannot "vectorize" part of the code, there are still ways to optimize, e.g. by eliminating function calls and implementing in place operations, by reducing the number of intermediary variables, by working along dimensions that make computations faster, ..
You exported/attached the summary, but I think that you saw the full report as well, with colors etc. If you enter functions from the MATLAB base package or toolboxes (which you cannot always do), you may realize that some are very slow because of tests implemented internally like calls to ISMATRIX or conversions from string to number that perform a lot of tests. If you see that, it can be advantageous to call more specific functions which involve less testing, or to build your own version of these functions without implementing any test (if you think that they are not relevant to your case).
Jan
Jan am 2 Aug. 2015
An integration with 1.9e6 function calls is suspicious. Do you integrate over a very long period of time or are the integration tolerances set to tiny values? In both cases the accumulated rounding errors must be taken into account and dominate the local discretization errors. Another problem could be, that the function to be integrated is stiff, while your ODE78 solver is designed for non-stiff functions. This would affect the accuracy severely. Or you have discontinuities in the function and the stepsize control fails.
So you see, that there can be many sources for a slow processing time apart from the old rumor, that Matlab processes loops slowly.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by