note 101171 deleted from control-structures.goto by danbrown
| From: | danbrown@php.net | Date: | Tue, 30 Nov 2010 21:50:59 +0000 |
| Subject: | note 101171 deleted from control-structures.goto by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174499@lists.php.net to get a copy of this message | ||
Note Submitter: mewsterus at gmail dot com
----
@tweston: Your code actually mimics a do {} while loop, not a plain while {} loop. This is what you
were looking for:
<?php
$start = microtime(true);
$i = 0;
StartOfLoop:
if(!($i < 1000000)) goto EndOfLoop;
$i++;
goto StartOfLoop;
EndOfLoop:
echo microtime(true) - $start.PHP_EOL;
?>
And of course it's as fast as a while loop. It might even be faster. Every single control
structure except for the basic "if" (even "else") is ultimately implemented as
GOTO statements. PHP probably doesn't do this, it probably passes the control structures onto
C, but the C compiler translates all of the control statements to GOTO statements. The reason it is
bad coding practice to do this manually is because while a compiler only has to define the labels
once and stick with it, as humans writing ever-changing computer code, we will make mistakes in the
future when modifying the code. It's less likely we'll make a mistake with well-defined
control structures than with GOTO statements, especially with the older languages and machine code
where it might be defined as a code offset rather than a label.