php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #53422 Foreach faster than in_array() and array_search()
Submitted: 2010-11-29 13:02 UTC Modified: 2010-11-29 15:40 UTC
From: rahul dot pache at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: ubuntu 10.04
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rahul dot pache at gmail dot com
New email:
PHP Version: OS:

 

 [2010-11-29 13:02 UTC] 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,


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-29 15:39 UTC] salathe@php.net
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 ==.
 [2010-11-29 15:40 UTC] salathe@php.net
-Status: Open +Status: Bogus
 [2010-11-29 15:40 UTC] salathe@php.net
Not a documentation bug -> bogus.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sat Oct 10 20:00:02 2026 UTC