|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-11-27 04:12 UTC] scottmac@php.net
-Status: Open
+Status: Bogus
[2010-11-27 04:12 UTC] scottmac@php.net
[2010-11-27 08:01 UTC] goetas at lignano dot it
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 06:00:01 2025 UTC |
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