note 97378 deleted from control-structures.switch by danbrown
| From: | danbrown@php.net | Date: | Fri, 16 Apr 2010 16:50:49 +0000 |
| Subject: | note 97378 deleted from control-structures.switch by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168326@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
Notice that "if" is (Tested on PHP-5.3.0 with 4 cases and default) around 8.8% faster than
"switch".
<?php
function sTest($a, $b, $c, $d) {
switch(true) {
case $a: return $a;
case $b: return $b;
case $c: return $c;
case $d: return $d;
default: return false;
}
}
function iTest($a, $b, $c, $d) {
if($a) return $a;
else if($b) return $b;
else if($c) return $c;
else if($d) return $d;
else return;
}
$microtime = microtime(true);
$testFunction = $argv[1];
for($i = 0; $i < 655360; $i++) {
for($e = 0; $e < 8; $e++) $testFunction($e == 0, $e == 1, $e == 2, $e == 3);
}
echo "{$testFunction} used " . (microtime(true) - $microtime) . " Seconds for
Calculation...\n";
?>