Is there a method of significant preallocation beyond zeros?

2 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by