Simple Code taking too much time

41 vues (au cours des 30 derniers jours)
Mohammad Kabalan
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))
while (y<=ceil(((yT+8*tXD)-Y1)/dY))
if (abs(sqrt(Tdist)/tXD) < 8)
*** dS(x,y)=dS(x,y)+dSi;***
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 commentaires
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.


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