Is there a method of significant preallocation beyond zeros?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Kelly Catlin
el 24 de Mzo. de 2018
Comentada: John D'Errico
el 24 de Mzo. de 2018
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;
0 comentarios
Respuesta aceptada
Walter Roberson
el 24 de Mzo. de 2018
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 comentario
John D'Errico
el 24 de Mzo. de 2018
Actually, what was done was worse than not preallocating at all.
Más respuestas (0)
Ver también
Categorías
Más información sobre Geographic Plots en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!