php.net |  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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
21 + 9 = ?
Subscribe to this entry?

 
 [2010-11-26 14:44 UTC] goetas at lignano dot it
Description:
------------
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:
---------------
<?php

// 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);

// TESTS

// 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-27 04:12 UTC] scottmac@php.net
-Status: Open +Status: Bogus
 [2010-11-27 04:12 UTC] scottmac@php.net
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: Tue Apr 23 14:01:31 2024 UTC