Loop until condition is true

Andrea Griffini agriff at tin.it
Sat Jun 18 11:02:37 EDT 2005


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

>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
   {
     /* 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

     <initialize>
L1:
     <evaluate condition>
     je L2
     <body>
     jmp L1
L2:

Instead the do-while loop would have been

L1:
     <body>
     <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.

Andrea



More information about the Python-list mailing list