php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30295 array_key_exists() makes a copy of the given array
Submitted: 2004-09-30 23:23 UTC Modified: 2006-01-01 21:54 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: oliver at burtchen dot com Assigned:
Status: Closed Package: Performance problem
PHP Version: 5CVS, 4CVS (2005-06-20) OS: *
Private report: No CVE-ID:
 [2004-09-30 23:23 UTC] oliver at burtchen dot com
Description:
------------
it looks loke array_key_exists() makes a copy of the given array if there is a reference to that array. if there is no reference, everything works well.

Reproduce code:
---------------
// just to get the time in microseconds
function getmicrotime() {
   list($usec, $sec) = explode(" ",microtime());
   return ((float)$usec + (float)$sec);
}


$cnt = 2000;
$a = array_fill(0, $cnt, 'foo');


// this works as aspected very fast

$mtime = getmicrotime();

for ($i = 0; $cnt > $i; $i++)
	array_key_exists($i, $a);

echo getmicrotime() - $mtime . '<br>';


$b =& $a;  // making a reference, but not using it


// this will take a huge amount of time, but it's the same code as above

$mtime = getmicrotime();

for ($i = 0; $cnt > $i; $i++)
	array_key_exists($i, $a);

echo getmicrotime() - $mtime . '<br>';



Expected result:
----------------
the two loops should work the same.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-01 01:17 UTC] helly@php.net
for a short term solution use 
array_key_exists($i, &$a);
 [2004-10-01 01:50 UTC] oliver at burtchen dot com
Using array_key_exists($i, &$a) is my solution for now, too. It works okay. Happy debugging...  ;-)
 [2006-01-01 21:54 UTC] iliaa@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Thu Apr 17 18:02:13 2014 UTC