3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php define('NUMBER_OF_THINGS', 100000); class Something{ const SOME_MAX_VALUE = 100; protected $field; public function setField($value){ $this->field = $value; return $this; } public function getField(){ return $this->field; } } $aBunchOfThings = array(); for($i=0; $i<NUMBER_OF_THINGS; $i++){ $aThing = new Something(); $aBunchOfThings[] = $aThing->setField(rand(1, Something::SOME_MAX_VALUE)); } function searchForeach($array, $needle){ $results = array(); foreach($array as $key => $thing){ if($thing->getField() == $needle){ $result[] = $needle; } } return $result; } function searchArrayFilter($array, $needle){ return array_filter($array, function(&$thing) use (&$needle){ return $thing->getField() == $needle; }); } function uniqueForeach($array){ $results = array(); foreach($array as $key => $thing){ $uniqueThing = true; foreach($results as $result){ if($result->getField() == $thing->getField()){ $uniqueThing = false; break; } } if($uniqueThing){ $result[] = $thing; } } return $result; } function uniqueArrayMap($array){ return array_intersect_key($array, array_unique(array_map(function (&$thing) { return $thing->getField(); }, $array))); } $needle = 50; $start = microtime(true); $result = searchForeach($aBunchOfThings, $needle); $durationForeach = microtime(true) - $start; $start = microtime(true); $result = searchArrayFilter($aBunchOfThings, $needle); $durationArrayFilter = microtime(true) - $start; $start = microtime(true); $result = uniqueForeach($aBunchOfThings); $durationUniqueForeach = microtime(true) - $start; $start = microtime(true); $result = uniqueArrayMap($aBunchOfThings); $durationUniqueArrayMap = microtime(true) - $start; echo "Function | Time\n"; echo "-----------------------------------------\n"; echo "searchForeach | $durationForeach\n"; echo "searchArrayFilter | $durationArrayFilter\n"; echo "uniqueForeach | $durationUniqueForeach\n"; echo "uniqueArrayMap | $durationUniqueArrayMap\n"; ?>
based on 8v719
Output for 7.1.0
Function | Time ----------------------------------------- searchForeach | 0.010612964630127 searchArrayFilter | 0.021213054656982 uniqueForeach | 0.013200998306274 uniqueArrayMap | 0.10517501831055
Output for 7.0.14
Function | Time ----------------------------------------- searchForeach | 0.010333061218262 searchArrayFilter | 0.022058963775635 uniqueForeach | 0.014835119247437 uniqueArrayMap | -0.092849016189575
Output for 7.0.10
Function | Time ----------------------------------------- searchForeach | 0.0080459117889404 searchArrayFilter | 0.01730489730835 uniqueForeach | 0.01658821105957 uniqueArrayMap | 0.1041419506073
Output for 7.0.9
Function | Time ----------------------------------------- searchForeach | 0.0073299407958984 searchArrayFilter | 0.015919923782349 uniqueForeach | 0.032816171646118 uniqueArrayMap | 0.12660002708435
Output for 7.0.8
Function | Time ----------------------------------------- searchForeach | 0.0066370964050293 searchArrayFilter | 0.014944076538086 uniqueForeach | 0.010546922683716 uniqueArrayMap | 0.092989921569824
Output for 7.0.7
Function | Time ----------------------------------------- searchForeach | 0.0067470073699951 searchArrayFilter | 0.015141010284424 uniqueForeach | 0.026585817337036 uniqueArrayMap | 0.11894798278809
Output for 7.0.6
Function | Time ----------------------------------------- searchForeach | 0.009857177734375 searchArrayFilter | 0.021265983581543 uniqueForeach | 0.010977029800415 uniqueArrayMap | 0.094440937042236
Output for 7.0.5
Function | Time ----------------------------------------- searchForeach | 0.0066201686859131 searchArrayFilter | 0.018502950668335 uniqueForeach | 0.059071063995361 uniqueArrayMap | 0.17143988609314
Output for 7.0.4
Function | Time ----------------------------------------- searchForeach | 0.0068180561065674 searchArrayFilter | 0.017771005630493 uniqueForeach | 0.056416034698486 uniqueArrayMap | 0.1670401096344
Output for 7.0.3
Function | Time ----------------------------------------- searchForeach | 0.0089929103851318 searchArrayFilter | 0.022521018981934 uniqueForeach | 0.011391878128052 uniqueArrayMap | 0.14843702316284
Output for 7.0.2
Function | Time ----------------------------------------- searchForeach | 0.0084218978881836 searchArrayFilter | 0.017849206924438 uniqueForeach | 0.061008930206299 uniqueArrayMap | 0.1778039932251
Output for 7.0.1
Function | Time ----------------------------------------- searchForeach | 0.0070090293884277 searchArrayFilter | 0.019073009490967 uniqueForeach | 0.061092138290405 uniqueArrayMap | 0.17124605178833
Output for 7.0.0
Function | Time ----------------------------------------- searchForeach | 0.0076420307159424 searchArrayFilter | 0.019284009933472 uniqueForeach | 0.047813892364502 uniqueArrayMap | 0.17728519439697
Output for hhvm-3.12.0
Function | Time ----------------------------------------- searchForeach | 0.020832061767578 searchArrayFilter | 0.048730134963989 uniqueForeach | 0.014019966125488 uniqueArrayMap | 0.049866914749146
Output for hhvm-3.10.0
Function | Time ----------------------------------------- searchForeach | 0.028342008590698 searchArrayFilter | 0.050940036773682 uniqueForeach | 0.023815870285034 uniqueArrayMap | 0.058032989501953
Output for 5.4.0 - 5.6.28
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 32 bytes) in /in/NaefP on line 58
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 71 bytes) in /in/NaefP on line 25
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/NaefP on line 34
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/NaefP on line 34
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/NaefP on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/NaefP on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/NaefP on line 5
Process exited with code 255.