Edit report at http://bugs.php.net/bug.php?id=53416&edit=1

 ID:                 53416
 Updated by:         scott...@php.net
 Reported by:        goetas at lignano dot it
 Summary:            iterate over array of complex objects slower than
                     array of simple objects
-Status:             Open
+Status:             Bogus
 Type:               Bug
 Package:            Performance problem
 Operating System:   Ubuntu Server 10
 PHP Version:        5.3.3
 Block user comment: N
 Private report:     N

 New Comment:

foreach() copies the array to work on. Hence why a complex object takes
longer to 

iterate as it takes longer to copy.


Previous Comments:
------------------------------------------------------------------------
[2010-11-26 14:44:50] 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


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=53416&edit=1

Reply via email to