Doc #53422 [NEW]: Foreach faster than in_array() and array_search()
| From: | rahul dot pache at gmail dot com | Date: | Mon, 29 Nov 2010 12:02:51 +0000 |
| Subject: | Doc #53422 [NEW]: Foreach faster than in_array() and array_search() | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-5593@lists.php.net to get a copy of this message | ||
From:
Operating system: ubuntu 10.04
PHP version: Irrelevant
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:Foreach faster than in_array() and array_search()
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 bug report at http://bugs.php.net/bug.php?id=53422&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=53422&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=53422&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=53422&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=53422&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=53422&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=53422&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=53422&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=53422&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=53422&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=53422&r=support
Expected behavior: http://bugs.php.net/fix.php?id=53422&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=53422&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=53422&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=53422&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=53422&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=53422&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=53422&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=53422&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=53422&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=53422&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=53422&r=mysqlcfg