Doc #53422 [Opn]: Foreach faster than in_array() and array_search()

From: Date: Mon, 29 Nov 2010 14:39:28 +0000
Subject: Doc #53422 [Opn]: Foreach faster than in_array() and array_search()
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-5594@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=53422&edit=1 ID: 53422 Updated by: salathe@php.net Reported by: rahul dot pache at gmail dot com Summary: Foreach faster than in_array() and array_search() Status: Open Type: Documentation Problem Package: Documentation problem Operating System: ubuntu 10.04 PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Where is that stated in the in_array() documentation? Try using the "strict" parameter for in_array()/array_search() or relaxing the if() to use ==. Previous Comments: ------------------------------------------------------------------------ [2010-11-29 13:02:51] rahul dot pache at gmail dot com Description: ------------ --- From manual page: http://www.php.net/function.in-array --- <?php clearstatcache(); $time_start = microtime(true); // generate an array $arr = array(); for($i=0; $i<100000; $i++) { $key = rand(0, 100000000); $val = rand(0, 100000000); $arr["{$key}"] = "{$val}"; } $time_end = microtime(true); $time = $time_end - $time_start; //round(, 6); echo "Time for cretion of array: {$time} seconds"; clearstatcache(); ///// --------------------------------------------------------------- $time_start = microtime(true); // Record time // search using foreach function foreach ($arr as $key=>$val) { if($val === "1000") { echo "<br />Found"; } } // Display time $time_end = microtime(true); $time = $time_end - $time_start; //round($time_end - $time_start, 4); echo "<br />Time for FOREACH of array: {$time} seconds"; /// --------------------------------------------------------------- $time_start = microtime(true); // Record time // search using in_array function in_array("1000", $arr); // Display time $time_end = microtime(true); $time = $time_end - $time_start; //round($time_end - $time_start, 4); echo "<br />Time for in_array() of array: {$time} seconds"; clearstatcache(); /// --------------------------------------------------------------- $time_start = microtime(true); // Record time // search using in_array function array_search("1000", $arr); // Display time $time_end = microtime(true); $time = $time_end - $time_start; //round($time_end - $time_start, 4); echo "<br />Time for array_search() of array: {$time} seconds"; clearstatcache(); phpinfo(); ?> Output: Time for cretion of array: 0.2615659236908 seconds Time for FOREACH of array: 0.013147115707397 seconds Time for in_array() of array: 0.021516084671021 seconds Time for array_search() of array: 0.021377086639404 seconds Problem: Indigenous functions are supposed to be much faster than foreach or any external loop (this is what I read in the book Zend PHP 5 Certification Study Guide), but its twice slower here, ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=53422&edit=1

« previous php.doc.bugs (#5594) next »