Are array_* functions faster than loops?

I was discussing the speed of filtering an array with a colleague and I had been under the assumption that using PHP array_* functions are considerably faster than using a loop (foreach, for, while) . I could not find evidence of that when I was doing some Google searches though so I decided to do a little bit of a speed test on my own.

For all of these tests I set up the following beforehand:

ini_set('memory_limit', '500M');
$data = range(0, 1000000);

Test 1: array_filter vs loops

| Type | Time | | ---- | ---- | | foreach | 0.37 | | while | 0.58 | | for | 0.61 | | array_filter | 0.74 |

// array_filter loop average 0.74 seconds
$start = microtime(true);
$data = array_filter($data, function ($item) {
    return $item%2;
});
$end = microtime(true);

echo $end - $start;

// Foreach loop average 0.37 seconds
$start = microtime(true);
$newData = array();
foreach ($data as $item) {
    if ($item%2) {
        $newData[] = $item;
    }
}
$end = microtime(true);

echo $end - $start;

// For loop average 0.61 seconds
$start = microtime(true);
$newData = array();
$numItems = count($data);
for($i=0;$i<=$numItems;$i++) {
    if ($data[$i]%2) {
        $newData[] = $data[$i];
    }
}
$end = microtime(true);

echo $end - $start;

// While loop average 0.58 seconds
$start = microtime(true);
$newData = array();
$numItems = count($data);
$i = 0;
while ($i <= $numItems) {
    if ($data[$i]%2) {
        $newData[] = $data[$i];
    }
    $i++;
}
$end = microtime(true);

echo $end - $start;

Test 2: array_map vs loops

| Type | Time | | ---- | ---- | | foreach | 0.65 | | while | 0.69 | | for | 0.76 | | array_filter | 1.38 |

// array_map average 1.38 seconds
$start = microtime(true);
$data = array_map(function ($item) {
    return $item+1;
}, $data);
$end = microtime(true);

echo $end - $start;

// For loop average 0.65 seconds
$start = microtime(true);
$newData = array();
foreach ($data as $item) {
    $newData[] = $item+1;
}
$end = microtime(true);

echo $end - $start;

// For loop average 0.76 seconds
$start = microtime(true);
$newData = array();
$numItems = count($data);
for($i=0;$i<=$numItems;$i++) {
    $newData[] = $data[$i]+1;
}
$end = microtime(true);

echo $end - $start;

// While loop average 0.69 seconds
$start = microtime(true);
$newData = array();
$numItems = count($data);
$i = 0;
while ($i <= $numItems) {
    $newData[] = $data[$i];
    $i++;
}
$end = microtime(true);

echo $end - $start;

Test 3: array_walk vs loops

| Type | Time | | ---- | ---- | | foreach | 0.65 | | while | 0.69 | | for | 0.72 | | array_filter | 0.76 |

// array_walk average 0.72 seconds
$start = microtime(true);
$data = array_walk($data, function ($item) {
    return $item+1;
});
$end = microtime(true);

echo $end - $start;

// Foreach loop average 0.65 seconds
$start = microtime(true);
$newData = array();
foreach ($data as $item) {
    $newData[] = $item+1;
}
$end = microtime(true);

echo $end - $start;

// For loop average 0.76 seconds
$start = microtime(true);
$newData = array();
$numItems = count($data);
for($i=0;$i<=$numItems;$i++) {
    $newData[] = $data[$i]+1;
}
$end = microtime(true);

echo $end - $start;

// While loop average 0.69 seconds
$start = microtime(true);
$newData = array();
$numItems = count($data);
$i = 0;
while ($i <= $numItems) {
    $newData[] = $data[$i];
    $i++;
}
$end = microtime(true);

echo $end - $start;

End Notes

I was incorrect when I thought that array_* functions are faster! Albeit the speed difference is pretty negligible when talking about using it once or twice during page execution. I won't stop using the array_* functions because of my findings, they still offer a cleaner way of writing the code. I will only second guess using them when I am writing code that has the potential to be processed thousands of times.

I tested these with a pretty basic data set. Speed may vary depending on the type of data being used as well as the version of PHP (I used 5.5.4).

If you have any feedback for me, I'd love to hear it - corrections, alternative paths, you name it! Send me an email levi@levijackson.xyz