Bug #53416 [NEW]: iterate over array of complex objects slower than array of simple objects

From: Date: Fri, 26 Nov 2010 13:44:51 +0000
Subject: Bug #53416 [NEW]: iterate over array of complex objects slower than array of simple objects
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-156489@lists.php.net to get a copy of this message
From:             
Operating system: Ubuntu Server 10
PHP version:      5.3.3
Package:          Performance problem
Bug Type:         Bug
Bug description:iterate over array of complex objects slower than array of simple objects

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

-- 
Edit bug report at http://bugs.php.net/bug.php?id=53416&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=53416&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=53416&r=trysnapshot53
Try a snapshot (trunk):              http://bugs.php.net/fix.php?id=53416&r=trysnapshottrunk
Fixed in SVN:                        http://bugs.php.net/fix.php?id=53416&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=53416&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=53416&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=53416&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=53416&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=53416&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=53416&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=53416&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=53416&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=53416&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=53416&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=53416&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=53416&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=53416&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=53416&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=53416&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=53416&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=53416&r=mysqlcfg



Thread (3 messages)

« previous php.bugs (#156489) next »