How to speed up my while loops
Afficher commentaires plus anciens
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 commentaires
I didn't look at your code carefully, but in my experience, while loops are more difficult to vectorize since you're looking for a condition to be met before changing behavior. In other words, you don't know a priori how many iterations are needed. Oftentimes, as you've seen in the blog, for loops can be completely replaced by a single vectorized line of code because they don't have this limitation.
One way around this is (could be) to vectorize the code in the while loop for a large swath of iterations, then check the results to see if your while conditions are met. If you make the initial swath large enough, you only need one iteration. Then you delete the results beyond the iteration that met your criteria.
One of the biggest speed-eaters is failure to pre-allocate arrays. Even if you don't know how big an array will be, you can over-allocate it and trim afterward. It's my understanding that when you append a single value onto an array, in terms of memory use, MATLAB creates a copy with the new value added rather than simply adding another element to the array. If the array already exists because you pre-allocated it, MATLAB doesn't have to make copies, but just fill in the elements. Hope that helps.
Felix Lauwaert
le 1 Août 2015
Jon
le 2 Août 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
le 2 Août 2015
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!