the time cost for the same m-file changed largely

Hi, I met a new problem recently.
When I just opened the matlab R2019b program, it needs about 100 secs to run a m-file. The main time cost is in some loop calculation.
However, when I run the same m-file for the 2nd time and then after, it would cost about 700 secs. I tried several times and also added 'clear' in the beginning of the program. However, it's still quite slow.
Have anyone ever met the same problem? Could please give some suggestions?
Thank you very much!

2 Comments

Can you post a small piece of code which reproduces the problem?
h p's incorrectly posted "Answer" moved here:
Thank you very much for all of your replies.
for a=a1:da:a2;
ia=ia+1;ib=0;
for b=0:db:2*pi;
ib=ib+1;
for i=1:Nt,
P=zeros(N);
t=dT*(i-1);
x_t=exp(-abs((t-T0)./w_t).^nDt).*x;
iX=round(a*cos(b)*(t)/dX);iY=round(a*sin(b)*(t)/dY);
iX1=(1+sign(iX+.1))*iX/2; iX2=(1-sign(iX+.1))*iX/2; iY1=(1+sign(iY+.1))*iY/2; iY2=(1-sign(iY+.1))*iY/2;
P( 1+iY1:N+iY2, 1+iX1:N+iX2 )=x_t( 1-iY2:N-iY1, 1-iX2:N-iX1 );
I(i)=sum(sum(P.*Spot));
if I(i)<I(1)/100, I(i+1:Nt)=0; break; end;
end
t=dT*(0:Nt-1);
S(ia,ib)=(sum(I)-I(1)/2)*dT;
end
end
the above is the code.
I didn't plot inside the loop. I also added 'clearvars; close all'; still the same.
I tested the time in matlab, and "x_t=exp(-abs((t-T0)./w_t).^nDt).*x " cost nearly 90% of the time. However, x is only a 100*100 matrix.
Also, when I used win8 and matlab 2014a, the time cost was always ~100s. After I updated to win10 and matlab 2019b, the time cost increased largely.

Sign in to comment.

Answers (1)

Hi,
Have you done any memory allocation for your loop calcs?
Have you moved all of plot commands outside of the loop? Plot takes lots of time to process.
Dont save all calcs from every iteration within the loop, unless they are necessary.
Start your code with: clearvars; close all
"clear all" is not recommended unless it is necessary. Instead of it, use "clearvars" or "clear variables"
Good luck.

Tags

Asked:

h p
on 27 May 2021

Commented:

on 28 May 2021

Community Treasure Hunt

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

Start Hunting!