Re:The Beauty of Bresenham's Algorithm
by nitrogen on Tuesday August 07, 2012 @09:11PM
(#4353333)
Because while(1) still has a condition that is checked on every loop iteration when optimizations are disabled. for(;;) can be read as "forever".
[ Parent
| on HN ]
FWIW, GCC and Perl generate the same code for both.
http://stackoverflow.com/questions/885908/while-1-vs-for-is-...
Interesting. Looks like they'd be the same performance wise, since
for (;;);
is the same as
{ while (true) { ; ; } }
I originally asked this question cos it's used this way in first C program in the link. Bit of a tangent though :P
Re:The Beauty of Bresenham's Algorithm
by WalterBright on Wednesday August 08, 2012 @12:47AM
(#4353964)
That issue disappeared before the 1980's. Check your compiler.
Re:The Beauty of Bresenham's Algorithm
by jejones3141 on Wednesday August 08, 2012 @12:11AM
(#4353854)
Any compiler/interpreter worth its salt will notice when an expression being tested in a conditional is constant and generate code that doesn't actually test it at run time. for (;;) is arguably more "idiomatic" C, though.
for(;;), being equivalent to for(;1;), also has that condition that has to be checked. So, it all boils down to how compilers treat the two.
And to add oil to the fire, here are some even 'better' ways to do this:
do {
...
} while(1);
At least that fits the common macro pattern "do{...}while(0)" (http://stackoverflow.com/questions/257418/do-while-0-what-is...).Of course, all of these are inferior to:
considered:
...
goto considered;I do doubt that even for a for(;;), a jmp (x86) is use instead of a conditional jump like jz.
Compilers are smart! For common things like this, they will easily optimise away any conditional jumps.