Which is more efficient for iterative programs: while loops or terminating with an if statement and break command?
Mostrar comentarios más antiguos
Just as the title of the question says, which method is more efficient, both in terms of memory and CPU usage. I'm sure this is common question, but I've not been able to find the right search terms to get an answer..
Sample code is provided below:
%while loop example
i=1;
criteria(i)=0;
while criteria(i)==0 %set an end criteria
for i=someArray
[output(i),criteria(i)]=fcn(input(i));%when fcn meets threshold, it outputs criteria=1
end
end
%if statement with break
for i=someArray
[output(i),criteria]=fcn(input(i));%same function as above
if criteria(i)==1
break
end
end
Also, out of curiosity, if there is a difference in efficiency, why is that the case?
Thanks in advance!
4 comentarios
per isakson
el 8 de Dic. de 2020
Test it with timeit
What is the point of that WHILE loop? It does nothing! Your first example makes no sense, and does not use the WHILE functionality for anything at all. It is certainly not at all equivalent to the second example.
If you really want a WHILE loop that is similar to your second example, it would be something more like this:
k = 1;
i = someArray(k);
criteria(i)=0;
while ~criteria(i)
i = someArray(k);
[output(i),criteria(i)]=fcn(input(i));
k = k+1;
end
If someArray is really just 1:N then of course you can simplify this too.
All of these constructs are very inefficient anyway, due to lack of preallocation of the output arrays. Any potential tiny tiny tiny difference between using equivalent (i.e. not like your examples) FOR and WHILE loops is going to be hugely outweighed by the lack of preallocation and the operations that you are performing inside the loop. If you want to learn how to write efficient MATLAB code, this is certainly not the place the start.
Michael Vladimirov
el 8 de Dic. de 2020
Especially for 10^6 iterations it will be very important to use pre-allocation:
timeit(@foo)
timeit(@bar)
function foo
x=[];
for n=1:10^6
x(n)=rand;
end
end
function bar
x=zeros(1,10^6);
for n=1:10^6
x(n)=rand;
end
end
while with break might add or shave off a few parts in a thousand, pre-allocation shaves off two-thirds. Pre-allocation doens't have to reduces code readability for beginners, especially if you write a comment.
Respuestas (1)
Paul Hoffrichter
el 8 de Dic. de 2020
Editada: Paul Hoffrichter
el 8 de Dic. de 2020
0 votos
for the "%if statement with break":
Suppose someArray has a million elements in it. If, in the 10th iteration, criteria(i)==1, then you have broken out of the loop.
In the same scenario with the "%while loop example" double loop case, the inner for-loop continues for all million elements, which takes a little longer than the "%if statement with break".
Normally, you try to estimate the size of the arrays. But since you did not, then, on average, in the above scenario, you will have created only 10 elements of output and criteria, whereas in the while loop, you always create a million elements - a little larger memory required.
In your example, breaking out of the loop is reduces memory and cpu usage on average.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!