Slow FOR loop for 2nd iteration and after

2 views (last 30 days)
I'm running a code that takes seismograms and stacks them according to their nearest neighbor. The first time through this loop it runs in .28 seconds (the line "disp('time for RFbeamer loop')" near the bottom), but each iteration afterwards takes almost 2 seconds to run. It stays at about 2 seconds each time after, it doesn't keep increasing in time. Only the variables that went in the first time go in for the rest of the iterations. With that said, I cannot figure out why the 2nd-nth iterations take almost 2 seconds longer!
for ij=1:m;
tic
centroid=GOOD(ij).name
numleft=num2str(m-ij);
disp(['Number of months left is ' months_left '. Current low pass frequency is ' freq ' Hz. Number of stations left for part 2 is ' numleft '.'])
load(centroid);
sample_before=(data(3).SACT0/data(3).dt)-(window(1)/data(3).dt);
sample_after=(data(3).SACT0/data(3).dt)+(window(2)/data(3).dt);
centroid_vertical_data=data(3).data(sample_before:sample_after);
ttest=zeros(size(timelist));
dtest2=ttest;
dtest=ttest;
dtime=abs(timelist-timelist(ij));
dlats=6371*(stlats-stlats(ij))*pi/180;
dlons=(6371*cos(stlats*pi/180)).*(stlons-stlons(ij))*pi/180;
dd=sqrt(dlats.^2+dlons.^2);
inbeam1=find(dtime<15);
ttest(inbeam1)=ones(1,length(inbeam1));
inbeam_hole=find(dd<=ri);
dtest(inbeam_hole)=ones(1,length(inbeam_hole));
ttest_hole=ttest.*dtest;
inbeam_hole2=find(ttest_hole>0);
inbeam_donut=find(dd>ri & dd<=ro);
dtest2(inbeam_donut)=ones(1,length(inbeam_donut));
ttest_donut=ttest.*dtest2;
inbeam_donut2=find(ttest_donut>0);
icount=0;
for i=1:length(inbeam_hole2);
fname1=GOOD(inbeam_hole2(i)).name;
load(fname1);
fname1_vertical_data=data(3).data(sample_before:sample_after);
corrn=max(conv(detrend(centroid_vertical_data),fliplr(detrend(fname1_vertical_data))));
corrd1=max(conv(detrend(centroid_vertical_data),fliplr(detrend(centroid_vertical_data))));
corrd2=max(conv(detrend(fname1_vertical_data),fliplr(detrend(fname1_vertical_data))));
corr=corrn/sqrt((corrd1)*corrd2);
if corr>=corrcoeff;
icount=icount+1;
MT_hole(icount).name=fname1;
end
end
jcount=0;
for j=1:length(inbeam_donut2);
fname2=GOOD(inbeam_donut2(j)).name;
load(fname2);
fname2_vertical_data=data(3).data(sample_before:sample_after);
corrn=max(conv(detrend(centroid_vertical_data),fliplr(detrend(fname2_vertical_data))));
corrd1=max(conv(detrend(centroid_vertical_data),fliplr(detrend(centroid_vertical_data))));
corrd2=max(conv(detrend(fname2_vertical_data),fliplr(detrend(fname2_vertical_data))));
corr=corrn/sqrt((corrd1)*corrd2);
if corr>=corrcoeff;
jcount=jcount+1;
MT_donut(jcount).name=fname2;
end
end
whos
toc
disp('time for RFbeamer loop')
pause
tic
beambld_9comp_2011(MT_hole,MT_donut,centroid,ri,ro)
toc
disp('time for beambld loop')
pause
clear MT_donut
number_in_hole=icount;
number_in_donut=jcount;
keep GOOD m ij timelist stlats stlons freq months_left ri ro reference_dt cutwindow corrcoeff window
end
Sorry for the long code. Any help for my problem would be greatly appreciated, (along with any advice for speeding it up in general)!

Accepted Answer

Jan
Jan on 11 Feb 2012
You can use the profiler to find out, which lines are taking more time in the second run. E.g. create one profile report when the program stops after the first iteration, and one with running two iterations.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!