Using for: % Time elapsed: 0.0010001659 seconds. Using while: % Time elapsed: 0.026000023 seconds. The main reason that While is much slower is because the while loop checks the condition after each iteration, so if you are going to write this code, just use a for loop instead.
while loops scale the best for large arrays. for...of loops are hands down the fastest when it comes to small data sets, but they scale poorly for large data sets. . forEach() and for...of were close enough that neither side should hang their hat on performance alone over the other.
Use a for loop when you know the loop should execute n times. Use a while loop for reading a file into a variable. Use a while loop when asking for user input. Use a while loop when the increment value is nonstandard.
Answer: That's very debatable, while (true) is not a good idea because it makes it hard to maintain this code. But that's not bad since you may not always know the exit condition when you set up the loop or may have multiple exit conditions. However, it does require more care to prevent an infinite loop.
I prefer for(;;)
for two reasons.
One is that some compilers produce warnings on while(true)
(something like "loop condition is constant"). Avoiding warnings is always a good thing to do.
Another is that I think for(;;)
is clearer and more telling.
I want an infinite loop. It literally has no condition, it depends on nothing. I just want it to continue forever, until I do something to break out of it.
Whereas with while(true)
, well, what's true got to do with anything? I'm not interested in looping until true becomes false, which is what this form literally says (loop while true is true). I just want to loop.
And no, there is absolutely no performance difference.
Personally I use for (;;)
because there aren't any numbers in it, it's just a keyword. I prefer it to while (true)
, while (1)
, while (42)
, while (!0)
etc etc.
Because of Dennis Ritchie
I started using for (;;)
because that's the way Dennis Ritchie does it in K&R, and when learning a new language I always try to imitate the smart guys.
This is idiomatic C/C++. It's probably better in the long run to get used to it if you plan on doing much in the C/C++ space.
Your #define
won't work, since the thing being #define'd has to look like a C identifier.
All modern compilers will generate the same code for the two constructs.
It's certainly not faster in any sane compiler. They will both be compiled into unconditional jumps. The for version is easier to type (as Neil said) and will be clear if you understand for loop syntax.
If you're curious, here is what gcc 4.4.1 gives me for x86. Both use the x86 JMP instruction.
void while_infinite()
{
while(1)
{
puts("while");
}
}
void for_infinite()
{
for(;;)
{
puts("for");
}
}
compiles to (in part):
.LC0:
.string "while"
.text
.globl while_infinite
.type while_infinite, @function
while_infinite:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
.L2:
movl $.LC0, (%esp)
call puts
jmp .L2
.size while_infinite, .-while_infinite
.section .rodata
.LC1:
.string "for"
.text
.globl for_infinite
.type for_infinite, @function
for_infinite:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
.L5:
movl $.LC1, (%esp)
call puts
jmp .L5
.size for_infinite, .-for_infinite
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With