Some situation, we can use while loop or do-while loop interchangeably. One of my friend told me that such situation we should use do-while loop. Because it is faster than while. Can anyone give me proof for it?
-
Hadn't we had this discussion: http://stackoverflow.com/q/3347001/694576 ? – alk Mar 15 '12 at 07:24
-
please check the site for duplicates yourself before posting – Jens Gustedt Mar 15 '12 at 07:42
7 Answers
You are comparing apples and peaches.
A do-while
and while
loops are different in what functionality they provide.
do-while
always exectures once, while
only executes if condition is true.
In situations where you can use either there is no performance difference.
And if you have a doubt then profiling it yourself on your platform or analysing the assembly output of both is what you should do.

- 202,538
- 53
- 430
- 533
-
@Ais Just read my question once again. I said SOME SITUATIONS we can use not ALL SITUATIONS. – Mohammed H Mar 15 '12 at 07:24
-
@HabeebPerwad: Just read my answer once again. The THIRD PARAGRAPH to be specific. – Alok Save Mar 15 '12 at 07:27
Both loops are pretty much the same (except of the fact that a do-while is executed at least once), and in many cases compile to the same instructions, at least on x86.
The following code:
int main(int argv, char *argc[]) {
int a = 0;
while (a < 100) {
a++;
}
a = 0;
do {
a++;
}while(a < 100);
return 0;
}
Generates this assembly (using gcc -S -o temp.as temp.c
):
_main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
call ___main
movl $0, 12(%esp) >> while
jmp L2
L3:
addl $1, 12(%esp)
L2:
cmpl $99, 12(%esp)
jle L3
movl $0, 12(%esp) >> do-while
L4:
addl $1, 12(%esp)
cmpl $99, 12(%esp)
jle L4
movl $0, %eax
leave
ret

- 135,866
- 28
- 264
- 277
-
The code shows that while using one extra instruction 'jmp L2'. Even if it is only one instruction, we can conclude that do-while is faster than while, right? – Mohammed H Mar 15 '12 at 07:34
-
2One branch that executes once is NOT something you should think about anyway. It is WAY TOO MINOR to be considered as a slow factor. It only happens in the first time. – MByD Mar 15 '12 at 07:45
-
1And this is so minor change, which is MUCH LESS important than the code readability – MByD Mar 15 '12 at 07:47
No. The C standard does not provide requirements for execution speed, so it is impossible to prove in general.

- 39,818
- 11
- 97
- 141
-
There are obvious situations that won't be optimized, like external function calls not in the C library (since they may have side effects that the compiler can't assume anything about). If your loop guard is an external call, then in general, calling it twice will be about twice as expensive as calling it once. We can have a lot of basically correct intution about program performance, optimization effects notwithstanding. – Kaz Mar 15 '12 at 07:43
-
@Kaz: I'm not sure I see your point. I agree that for certain programs and certain compilers it is possible to sometimes predict performance; however, this hardly constitutes a "proof" (as HabeebPerwad asked for). – Mankarse Mar 15 '12 at 08:31
The difference between while and do-while is that do-while always executes the first time, without checking the condition. So assuming that the condition is actually true the first time, do-while saves this one check of the condition.
This is a micro-optimization at best.

- 811,555
- 193
- 1,581
- 1,452
-
There is no upper bound on how much of an optimization this can be because a loop guard can be arbitrarily expensive relative to the body of the loop and number of iterations. – Kaz Mar 15 '12 at 07:34
-
I believe it is more than only 1 instruction. since it only has one branch while the other loops have two (one conditional at the begging of the branch and one unconditional at the end), it give the compiler more freedom to optimize the code. – Mehdi Aug 21 '17 at 18:26
If you dont care about the different semantic (at least once vs. maybe none), there is no difference.

- 8,018
- 2
- 27
- 53
C semantic describes the behavior of an abstract machine in which issues of optimisation are irrelevant. (see C99 Standard, 5.1.2.3p1)

- 142,963
- 15
- 272
- 331
If it's a situation where the compiler can optimize it well, stick with the while
loop. Top-of-loop testing is clearer. The reason is rooted in formal verification. The while
loop tests the preconditions for the correct execution of the body. The do
loop executes a body once without testing anything, and then has to deal with the aftermath, so to speak. It's more tricky to reason about. We have to think about what the situation is as a result of the conditions that existed before the loop plus the effect of the unguarded execution of the first iteration.
If you have a loop guard which is expensive (like a call to some external function that can do I/O or who knows what else) then think about moving it down, if possible.

- 55,781
- 9
- 100
- 149