# Simple Code taking too much time

29 vues (au cours des 30 derniers jours)
Mohammad Kabalan le 28 Juin 2013
the code below is part of a large program and is looped through about 50 times total. I haven't worked on this code in a while but it was running fine. I made some very slight changes to other parts of the program and now it is taking too much time to run (it used to take around 2 hours for the 50 loops, now I leave it for around 16 hours and its is still in the 40t loop). below is the part of the code which is taking more than 99% of the run time. I used profiler and the line between asteriks is the one taking a very long time to run although it is a simple addition. this starts happening after the 20th loop roughly (even if I start at the 20th loop, I still have the issue).
while (x<=ceil(((xT+8*tXD)-X1)/dX))
y=max(floor(((yT-8*tXD)-Y1)/dY),1);
while (y<=ceil(((yT+8*tXD)-Y1)/dY))
Tdist=(x*dX+X1-xT)^2+(y*dY+Y1-yT)^2;
if (abs(sqrt(Tdist)/tXD) < 8)
dSi=((tFv*tdV)*1+ZZ(i,j)*cov))/(2*pi*tXD)*exp(-0.5*TdisttXD);
*** dS(x,y)=dS(x,y)+dSi;***
end
y=y+1;
end
x=x+1;
end
Any ideas as to what could be causing such delay in such a simple step. dS is already preallocated. I'm not an expert in MATLAB but id doesn't make sense for a simple addition step to take so long while more complicated steps are executed quicker.
##### 7 commentairesAfficher 5 commentaires plus anciensMasquer 5 commentaires plus anciens
Marc le 29 Juin 2013
Just one last random thought.... Is this some kind of finite difference scheme? Turning some PDEs into odes and using ode15s or something?
I ask because I ran profiler on something like that, where the function is 3000+ lines, using nested functions to get around global variables and it ran like 10X longer than tic/toc and internal clock calls with etime().
I never followed up on the profiler hiccup but you may want to add some calls to clock with etime and see if this compares well to the profiler.
Mahdi le 29 Juin 2013
Maybe the problem is that, that line is being called so much (which seems inevitable), because as you said it is just a simple addition.

Connectez-vous pour commenter.

### Réponses (1)

Guru le 3 Juil 2013
This simply looks to be a case where you are trying to make some result converge to an answer, and the system is not set up to converge nicely with the given settings.
The issue that you have is not a result of the code that is asterisked takes very long to run, it's that you have a nested while loop where the adjustments you are making simply do not converge to anything. MATLAB however will still try to optimize it, and to handle this problem you should use the optimization functions within the Optimization toolbox rather than try to use your own optimization routines unless you build in some kind of metric that detects there is no solution that it can converge to.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by