3v4l.org

run code in 300+ PHP versions simultaneously
<?php //Access every element once using array_filter() $objectArray = []; $objectNames = []; for($i = 0; $i < 1000; $i ++){ $objName = 'object_name_' . ($i + 1); $objectNames[] = $objName; $obj = new stdClass(); $obj->name = $objName; $obj->description = 'test description'; $obj->accessed = 0; $objectArray[] = $obj; } foreach($objectNames as $name){ $iterations = getObjectWithArray_Filter($name, $objectArray); } echo $iterations . " iterations using array_filter()<br/>\n"; //Access every element once using foreach(){} $objectArray = []; $objectNames = []; for($i = 0; $i < 1000; $i ++){ $objName = 'object_name_' . ($i + 1); $objectNames[] = $objName; $obj = new stdClass(); $obj->name = $objName; $obj->description = 'test description'; $obj->accessed = 0; $objectArray[] = $obj; } foreach($objectNames as $name){ $iterations = getObjectWithForeach($name, $objectArray); } echo $iterations . " iterations using foreach(){}<br/>\n"; //Access every element once using Associative array $objectArray = []; $objectNames = []; for($i = 0; $i < 1000; $i ++){ $objName = 'object_name_' . ($i + 1); $objectNames[] = $objName; $obj = new stdClass(); $obj->name = $objName; $obj->description = 'test description'; $obj->accessed = 0; $objectArray[] = $obj; } $associativeArray = []; foreach($objectArray as $object){ $associativeArray[$object->name] = $object; $object->accessed ++; } foreach($objectNames as $name){ $iterations = getObjectFromAssociativeArray($objName, $associativeArray); } echo $iterations . " iterations using associative array{}<br/>\n"; //================================================================= function getObjectWithArray_Filter($objectName, array $objectArray){ $myobjects = array_filter($objectArray, function($e) use($objectName) { $e->accessed ++; return strcmp($e->name, $objectName) == 0; }); $iterations = 0; foreach($objectArray as $object){ $iterations += $object->accessed; } return $iterations; } function getObjectWithForeach($objectName, array $objectArray){ $iterations = 0; $found = false; $count = 0; while(!$found){ $objectArray[$count]->accessed ++; if($objectArray[$count]->name === $objectName){ $found = true; } $count ++; } foreach($objectArray as $object){ $iterations += $object->accessed; } return $iterations; } function getObjectFromAssociativeArray($objectName, array $objectArray){ $iterations = 0; if($objectName === $objectArray[$objectName]->name){ $objectArray[$objectName]->accessed ++; } foreach($objectArray as $object){ $iterations += $object->accessed; } return $iterations; }
Output for 5.4.0 - 5.4.17, 5.4.21 - 5.4.45, 5.5.0, 5.5.3 - 5.5.6, 5.5.9 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.31, 7.3.0 - 7.3.18, 7.4.0 - 7.4.6
1000000 iterations using array_filter()<br/> 500500 iterations using foreach(){}<br/> 2000 iterations using associative array{}<br/>
Output for 5.4.18 - 5.4.19, 5.5.1 - 5.5.2, 5.5.7 - 5.5.8
1000000 iterations using array_filter()<br/> 500500 iterations using foreach(){}<br/>
Process exited with code 137.
Output for 5.4.20
1000000 iterations using array_filter()<br/>
Process exited with code 137.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/IUnA0 on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/IUnA0 on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/IUnA0 on line 3
Process exited with code 255.

preferences:
227.66 ms | 401 KiB | 340 Q