note 97378 added to control-structures.switch

From: Date: Fri, 16 Apr 2010 16:48:58 +0000
Subject: note 97378 added to control-structures.switch
Groups: php.notes 
Request: Send a blank email to php-notes+get-168325@lists.php.net to get a copy of this message
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"; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 84.166.24.185 ---- Manual Page -- http://www.php.net/manual/en/control-structures.switch.php Edit -- https://master.php.net/note/edit/97378 Del: integrated -- https://master.php.net/note/delete/97378/integrated Del: useless -- https://master.php.net/note/delete/97378/useless Del: bad code -- https://master.php.net/note/delete/97378/bad+code Del: spam -- https://master.php.net/note/delete/97378/spam Del: non-english -- https://master.php.net/note/delete/97378/non-english Del: in docs -- https://master.php.net/note/delete/97378/in+docs Del: other reasons-- https://master.php.net/note/delete/97378 Reject -- https://master.php.net/note/reject/97378 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168325) next »