Simple Code taking too much time

48 次查看(过去 30 天)
Mohammad Kabalan
Mohammad Kabalan 2013-6-28
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 个评论
Marc
Marc 2013-6-29
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
Mahdi 2013-6-29
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.

请先登录,再进行评论。

回答(1 个)

Guru
Guru 2013-7-3
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by