Why is it taking so long to execute program?

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 Comment

Stephen23
Stephen23 on 16 Apr 2016
Edited: Stephen23 on 16 Apr 2016
John D'Errico explains the difference in some detail. You can also read what The Mathworks (MATLAB) have to say about array preallocation here:

Sign in to comment.

 Accepted Answer

What do you think MATAB must do when you do something like this:
tic
clear n
n(1) = 1;
for i = 1:10000000
n(i) = i;
end
toc
Elapsed time is 1.212347 seconds.
First, MATLAB declares the variable n as a scalar.
Then it replaces n with a vector of length 2, copying every element in n to a new location, since the variable need to be dynamically reallocated.
Then it repeats the process, over and over again, with bigger and bigger vectors each time.
How long do you think that will take?
What will happen if you change the code simply to:
tic
clear n
n = NaN(1, 10000000);
n(1) = 1;
for i = 1:10000000
n(i) = i;
end
toc
Elapsed time is 0.207284 seconds.
Why is the second code different? I preallocated the vector n. One line was added. But having allocated space for the ENTIRE vector, now the vector need not be dynamically resized, reallocated with each element added. The vector now never changes size.
What did you do in your first example, that ran quickly? What was the VERY first line?
n(N_go)=ninf;?
What does that do? THINK!
clear n
N_go = 10;
n(N_go) = inf;
n
n =
0 0 0 0 0 0 0 0 0 Inf
So a vector is created with all zero elements. It was preallocated, by setting the LAST element to some value.
In the second code that you wrote, where did you start? What was the size of n at the beginning?
clear n
n(1) = 0;
whos n
Name Size Bytes Class Attributes
n 1x1 8 double
n was a scalar when you started that loop.

3 Comments

I'm sorry, I'm quite new to programming and I don't know stuff like that. I tried this, but it wouldn't help. Any more ideas?
No. The fact is, you did NOT try what I told you, as it would have helped. READ what I said above. Think about what is happening. Then think about why the second case I show is different.
Actually, I really understood what you said the first time :) When I added this line to my code, it really did no effect, but i guess the problem was somewhere else, since when I re-wrote the same code to other, new file it worked. Oh well, now everything is working, thank you for your answer.

Sign in to comment.

More Answers (0)

Categories

Products

Asked:

on 16 Apr 2016

Commented:

on 16 Apr 2016

Community Treasure Hunt

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

Start Hunting!