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

1 vue (au cours des 30 derniers jours)
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 commentaires
Michael Vladimirov
Michael Vladimirov le 8 Déc 2020
"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
Rik
Rik le 8 Déc 2020
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.

Connectez-vous pour commenter.

Réponses (1)

Paul Hoffrichter
Paul Hoffrichter le 8 Déc 2020
Modifié(e) : Paul Hoffrichter le 8 Déc 2020
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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by