How to speed up my while loops
조회 수: 13 (최근 30일)
이전 댓글 표시
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
Jon
2015년 8월 2일
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.
채택된 답변
Jan
2015년 8월 1일
편집: Jan
2015년 8월 1일
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
Cedric
2015년 8월 2일
편집: Cedric
2015년 8월 2일
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
2015년 8월 2일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!