Why is it taking so long to execute program?
Afficher commentaires plus anciens
Hello,
I have two similar programs, but their execution time is so different. This one executes very fast:
n(N_go)=ninf;
n(N_go-1)=ninf-epsi;
for m=(N_go-1):-1:2
n(m-1)=h^2/D*(B*n(m)^2-G)+2*n(m)-n(m+1);
end;
And this one takes very very long:
n(1)=n0;
n(2)=C/(2*D)*n(1)*h+n(1);
for m=2:1:N_go
n(m+1)=h^2/D*(B*n(m)^2-G)+2*n(m)-n(m-1);
end;
N_go=10000000 in both cases. Other parameters are also equal. Can someone explain to me, why second code is taking so long to execute?
1 commentaire
John D'Errico explains the difference in some detail. You can also read what The Mathworks (MATLAB) have to say about array preallocation here:
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Introduction to Installation and Licensing dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!