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_map 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
array_filter 0.72
for 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).

3 thoughts on “Are array_* functions faster than loops?”

  1. Great post Levi! I always thought that array_* functions were faster as well. Mike and I were just having this conversation and found this blog post, then realized that we all went to school together! I agree with your conclusion– I too will continue to use array_* functions for their cleanliness and readability unless the data set is large enough where those microseconds start adding up. I speculate that the slowdown is a result of the array_* functions having to invoke the anonymous function every iteration as opposed to the inline nature of foreach/loops.

  2. FYI future readers: The writer made a mistake. He is assigning the result to $data on each array_* loop, which i.e. half the array size on comparison 1.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>