Loop until condition is true

Andrea Griffini agriff at tin.it
Sat Jun 18 17:02:37 CEST 2005

On Sat, 18 Jun 2005 13:35:16 -0000, Grant Edwards <grante at visi.com>

>AFAICT, the main use for do/while in C is when you want to
>define a block of code with local variables as a macro:

When my job was squeezing most out of the CPU (videogame
industry) I remember that the asm code generated by

   while (sz-- > 0)
     /* do some stuff */

was indeed worse than

     /* do some stuff */
   } while (--sz);

because of the initial "empty-loop" test (conditional jumps
were bad, and forward conditional jumps were worse).
So where at least one iteration was guaranteed the do-while
loop was a better choice.

Also I've been told there were compilers that if using
for or while loops the generated code was

     <evaluate condition>
     je L2
     jmp L1

Instead the do-while loop would have been

     <evaluate condition>
     jne L1

I.e. the code was better *for each iteration* (one conditional
jump instead of one conditional jump and one inconditional jump).

I think compiler got better since then, even if I don't think
they already so smart to be able to infer the "one interation
guaranteed" property to avoid the initial test that often.


More information about the Python-list mailing list