|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53416 iterate over array of complex objects slower than array of simple objects
Submitted: 2010-11-26 14:44 UTC Modified: 2010-11-27 08:01 UTC
From: goetas at lignano dot it Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.3.3 OS: Ubuntu Server 10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: goetas at lignano dot it
New email:
PHP Version: OS:


 [2010-11-26 14:44 UTC] goetas at lignano dot it
I do not understand if is an error or not, but iterating over arrays with complex objects is slower than iterate over arrays made with more simple objects. 

In some case (but i can't reproduce it), iterate over arrays with 100 elements is 100x slower than iterate other array made of 100 simpler objects.

(sorry for my English)

Test script:

// create some arrays

 // integer array (1000 items)
$vals = range(0,1000);

// simple object array (1000 items)
$obj = array_map(function($v){ 
	$o = new stdClass();
	$o->v = $v;
	return $o; 
}, $vals);

// more complex object array (1000 items)
$objs = array_map(function($v)use(&$obj){ 
	$o = new stdClass();
	$o->res = array();
	for ($i = 0; $i<500; $i++){
		$o->res[$i] = $obj[$i];
	for (true; $i<1000; $i++){
		$o->{'res'.$i} = $obj[$i];
	return $o; 
}, $vals);


// 1. loop over integer array 
// 1000000 iterations
$t = microtime(1);
foreach ($vals as $o1){
	foreach ($vals as $o2){
echo microtime(1)-$t."\n";

// 2. loop over simple object array 
// 1000000 iterations 
// 15% slower than loop n.1
$t = microtime(1);
foreach ($obj as $o1){
	foreach ($obj as $o2){
echo microtime(1)-$t."\n";

// 3. loop over complex object array 
// 1000000 iterations
// 50% slower than loop n.1
$t = microtime(1);
foreach ($objs as $o1){
	foreach ($objs as $o2){
echo microtime(1)-$t."\n";

Expected result:
all loop  should take the same amount of time. (at most with slight differences)

Actual result:
loop n.3 is 50% slower than loop 1
loop n.3 is 30% slower than loop 2
in some case iterate over complex array can be 100x slower


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-27 04:12 UTC]
-Status: Open +Status: Bogus
 [2010-11-27 04:12 UTC]
foreach() copies the array to work on. Hence why a complex object takes longer to 
iterate as it takes longer to copy.
 [2010-11-27 08:01 UTC] goetas at lignano dot it
That should be true for the first loop.
But in the second and third loop, the array $obj or $objs should be made only of 1000 references of the real objects.
Iterating over the array of objects should copy only these references and not the entire objects set.

So the loop n.2 and n.3 should require the same amount of time.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 30 07:01:30 2024 UTC