Is there a method of significant preallocation beyond zeros?

조회 수: 3 (최근 30일)
Kelly Catlin
Kelly Catlin 2018년 3월 24일
댓글: John D'Errico 2018년 3월 24일
I have been calling a function with 83000 iterations (as below):
>>[t1data,t2data,Tdata,Ddata,lamdata] = example_run(2,0.1,25,1.01)
Yet I have let it run for the better part of a day without the program terminating. Is there a way (beyond preallocation with zeros, as I have already done) to decrease the run time significantly?
function [t1data,t2data,Tdata,Ddata,lamdata] = example_run(E0,sigma,F,tau)
%E0=2;sigma=0.1;F=25;tau=1.01; standard reference
%preallocation
Tdata = zeros(1, 100000);
t1data = zeros(1, 100000);
t2data = zeros(1, 100000);
kdata = zeros(1, 100000);
Ddata = zeros(1, 100000);
lamdata = zeros(1, 100000);
[Tc,Ts]=findTcTs(E0,sigma,F,tau);
%T=Ts+0.5;
%[t1,t2,lambda,D]=findt1t2lambdaD(E0,sigma,F,tau,T,Tc);
hold on;
T = Ts;
T0 = Tc;
for k = 1:83001
[t1,t2,lambda,D] = findt1t2lambdaD(E0,sigma,F,tau,T,T0);
Tdata = [Tdata,T];
t1data = [t1data,t1];
t2data = [t2data,t2];
kdata = [kdata,k];
Ddata = [Ddata,D];
lamdata = [lamdata, lambda];
T0 = t2;
T = T + 0.1;
plot(T,real(D), 'o')
%iterate by increments to use valid initial guess for t2 for each increment in findt1t2...
end
hold off;

채택된 답변

Walter Roberson
Walter Roberson 2018년 3월 24일
Tdata = [Tdata,T];
Don't do that. You already used
Tdata = zeros(1, 100000);
so each iteration you are expanding Tdata by putting a single new element after all of the zeros. That is not preallocation! Preallocation would be if you had used
Tdata = zeros(1, 100000);
with
Tdata(k) = T;
(It is not obvious why your 100000 does not match your 83001)
  댓글 수: 1
John D'Errico
John D'Errico 2018년 3월 24일
Actually, what was done was worse than not preallocating at all.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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