2

Is there a difference in speed between declaring an array variable before using it on the foreach loop?

e.g.

$variable = [1, 2, 3];

foreach ($variable as $value) {}

VS

foreach ([1, 2, 3] as $value) {}
1
  • 1
    There shouldn't be much as PHP creates a copy or sort of caches the array when it does foreach. This can produce some weird results when you modify it by refrence with $key => &$value and then change the array structure, i've seen it do some "spooky" stuff... lol Commented Feb 16, 2018 at 5:09

2 Answers 2

4

I did a small experiment

function measure($time, $func)
{
    $before = microtime(true);
    for($i = 0; $i < $time; $i++) $func();
    $after = microtime(true);
    return $after - $before;
}

var_dump(measure(10000000, function() {
    $variable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    foreach ($variable as $value) {}
}));  // 7.677169084549

var_dump(measure(10000000, function() {
    foreach ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as $value) {}
})); // 6.7569959163666

There are not much different in the speed.

Sign up to request clarification or add additional context in comments.

Comments

2

PHP creates a copy or caches the array, so for example doing this can expose this behaviour

$array = array("1" => "PHP code tester Sandbox Online",  
    "foo" => "bar", 5 , 5 => 89009, 
    "case" => "Random Stuff: " . rand(100,999),
    "PHP Version" => phpversion()
);

foreach( $array as $key => $value ){
   echo "Key: $key\n";
   if($key == "1") unset($array["case"]);   
}

print_r($array);

Outputs:

Key: 1
Key: foo
Key: 2
Key: 5
Key: case  //<-- what is this doing here
Key: PHP Version
Array
(
    [1] => PHP code tester Sandbox Online
    [foo] => bar
    [2] => 5
    [5] => 89009
    [PHP Version] => 5.6.18
)

Now what we see here is on index 1 we unset the key case and indeed it is not in the output of the array, however, we still get to loop the item. Now if you update the value by reference with

foreach( $array as $key => &$value ){

///...

This disappears

Key: 1
Key: foo
Key: 2
Key: 5
Key: PHP Version
Array
(
    [1] => PHP code tester Sandbox Online
    [foo] => bar
    [2] => 5
    [5] => 89009
    [PHP Version] => 5.6.18
)

You can try it in this sandbox

So what we learned from this is that during the first example, PHP has copied or cached the array data internally, so when we make the change to it's structure the foreach doesn't realize this. Because of this, your only extra cost is the variable assignment for the original array. So it doesn't really matter much if you create the array in the foreach, in a variable or in a method as the cost to do that compared to creating it in the foreach is very minor.

There is no extra per loop assignment, when creating it in the foreach or when calling a method in the foreach (this made more sense when i thought about it then when I say it). Basically it is not like when you call for($i=0; $i < count($b); $i++) where in this case count would be done on each iteration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.