Which is more efficient for iterative programs: while loops or terminating with an if statement and break command?

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

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.
"What is the point of that WHILE loop? It does nothing!"
Stephen Cobledick, I'm sorry about that, you're correct. Such is what happens when you make latenight posts and don't self-check before hitting submit.
With that said, thank you but I'm not terribly interested in preallocation and/or vectorization. I am, however, interested in learning if there is a performance difference between using while loops or break commands inside of if statements for implementing stopping criteria?
I'm writing a particular code primarily with the intention of making it as readable as possible, particularly for beginners. In cases where it runs 10^6 or more iterations multiplied by several dozen different input parameter combinations, potentially shaving a minute or two off by using a marginally faster stopping criteria would be nice, as this would not affect code readability for beginners
Especially for 10^6 iterations it will be very important to use pre-allocation:
timeit(@foo)
ans = 0.1561
timeit(@bar)
ans = 0.0455
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.

Iniciar sesión para comentar.

Respuestas (1)

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

Etiquetas

Preguntada:

el 8 de Dic. de 2020

Comentada:

Rik
el 8 de Dic. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by