Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Created October 15, 2011 20:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ocramius/1290076 to your computer and use it in GitHub Desktop.
Save Ocramius/1290076 to your computer and use it in GitHub Desktop.
PHP Benchmark for array_search against foreach
<?php
for($j = 0; $j < 10000; $j++) {
//test with element at the beginning of the array
$searchArray = array();
$searchArray[] = $element = new stdClass();
for($i = 0; $i < 100; $i++) {
$searchArray[] = new stdClass();
}
$found = false !== array_search($element, $searchArray, true);
//test with element at the center of the array
$searchArray = array();
for($i = 0; $i < 50; $i++) {
$searchArray[] = new stdClass();
}
$searchArray[] = $element = new stdClass();
for($i = 0; $i < 50; $i++) {
$searchArray[] = new stdClass();
}
$found = false !== array_search($element, $searchArray, true);
//test with element at the end of the array
$searchArray = array();
for($i = 0; $i < 100; $i++) {
$searchArray[] = new stdClass();
}
$searchArray[] = $element = new stdClass();
$found = false !== array_search($element, $searchArray, true);
}
<?php
for($j = 0; $j < 10000; $j++) {
//test with element at the beginning of the array
$searchArray = array();
$searchArray[] = $element = new stdClass();
for($i = 0; $i < 100; $i++) {
$searchArray[] = new stdClass();
}
foreach ($searchArray as $collectionElement) {
if ($element === $collectionElement) {
$found = true;
break;
}
}
//test with element at the center of the array
$searchArray = array();
for($i = 0; $i < 50; $i++) {
$searchArray[] = new stdClass();
}
$searchArray[] = $element = new stdClass();
for($i = 0; $i < 50; $i++) {
$searchArray[] = new stdClass();
}
foreach ($searchArray as $collectionElement) {
if ($element === $collectionElement) {
$found = true;
break;
}
}
//test with element at the end of the array
$searchArray = array();
for($i = 0; $i < 100; $i++) {
$searchArray[] = new stdClass();
}
$searchArray[] = $element = new stdClass();
foreach ($searchArray as $collectionElement) {
if ($element === $collectionElement) {
$found = true;
break;
}
}
}
Tested on PHP 5.3.8 on Mac OS 10.6.8.
These results are just micro-optimization, which is quite useless, but the foreach construct seems to be always slower than array_search.
I expected much better results, but I frankly don't know why I didn't get them this time. Unfortunately I don't have the previous benchmark anymore.
Marco-Pivettas-MacBook-Pro:~ ocramius$ #all 3 cases
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php array_search.php
real 0m2.900s
user 0m2.872s
sys 0m0.012s
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php foreach.php
real 0m3.115s
user 0m3.097s
sys 0m0.013s
Marco-Pivettas-MacBook-Pro:~ ocramius$ #just element at the beginning of the array
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php array_search.php
real 0m0.971s
user 0m0.951s
sys 0m0.010s
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php foreach.php
real 0m0.948s
user 0m0.933s
sys 0m0.011s
Marco-Pivettas-MacBook-Pro:~ ocramius$ #just element in the middle of the array
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php array_search.php
real 0m0.983s
user 0m0.960s
sys 0m0.009s
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php foreach.php
real 0m1.065s
user 0m1.048s
sys 0m0.012s
Marco-Pivettas-MacBook-Pro:~ ocramius$ #just element at the end of the array
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php array_search.php
real 0m0.978s
user 0m0.957s
sys 0m0.010s
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php foreach.php
real 0m1.124s
user 0m1.109s
sys 0m0.010s
Marco-Pivettas-MacBook-Pro:~ ocramius$ #all 3 cases
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php array_search-benchmark.php
real 0m2.900s
user 0m2.872s
sys 0m0.012s
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php foreach-benchmark.php
real 0m3.115s
user 0m3.097s
sys 0m0.013s
Marco-Pivettas-MacBook-Pro:~ ocramius$ #just element at the beginning of the array
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php array_search-benchmark.php
real 0m0.971s
user 0m0.951s
sys 0m0.010s
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php foreach-benchmark.php
real 0m0.948s
user 0m0.933s
sys 0m0.011s
Marco-Pivettas-MacBook-Pro:~ ocramius$ #just element in the middle of the array
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php array_search-benchmark.php
real 0m0.983s
user 0m0.960s
sys 0m0.009s
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php foreach-benchmark.php
real 0m1.065s
user 0m1.048s
sys 0m0.012s
Marco-Pivettas-MacBook-Pro:~ ocramius$ #just element at the end of the array
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php array_search-benchmark.php
real 0m0.978s
user 0m0.957s
sys 0m0.010s
Marco-Pivettas-MacBook-Pro:~ ocramius$ time php foreach-benchmark.php
real 0m1.124s
user 0m1.109s
sys 0m0.010s
@XedinUnknown
Copy link

@Ocramius, have you got by chance the results for PHP ^7.0?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment