Why does $a += 3 execute faster than $a = $a + 3?
Asked Answered
N

3

7

PHP manual states that:

Adding 3 to the current value of $a can be written '$a += 3'. This means exactly "take the value of $a, add 3 to it, and assign it back into $a". In addition to being shorter and clearer, this also results in faster execution.

I used to think that $a += 3 is merely syntax sugar for $a = $a + 3 and thus they should be equal in all respects.

Why does $a += 3 result in faster execution compared to $a = $a + 3?

Norm answered 5/7, 2013 at 16:18 Comment(6)
if nothing else, it's slightly less code to parse.Siloxane
How do you time it? It would be interesting to know...Roxieroxine
my guess is you can increment $a rather than change $a to a new variable which is just 3 digits largerConstituency
Bench-marked here if anyone is interested.Norris
its faster => unoptimized, translator doesn't analysis that one left operand is target operand, It means if $a += 3 faster then $a = $a + 3, $a = $a + 3 will be probably faster then $a = $a + $aRossiya
en.wikipedia.org/wiki/Augmented_assignment operator advantage.Cauterant
H
4

$a = $a + 3 adds 3 to $a in a temporary memory space, then assigns the result to $a; while $a += 3 adds 3 directly to $a; so the difference is a few bytes of memory for temporary storage, plus an assignment

Hug answered 5/7, 2013 at 16:22 Comment(1)
I'm not sure about called 3 times. $a = $a + 3: copy $a to temp memory space, add 3 to the value in temp memory space, override value of $a with value from temp memory space; $a += 3: add 3 to the value of $a - no need to copy $a to tempspace; no need to write tempspace value to $aHug
G
3

PHP is an interpreter, so, in order to have a good performance for good code, it must restrict itself to do not do "valid" complex opimizations (as compilers can do, because they have time for that).
Since the time of asembler, it is better to have =+ than its equivalent sum, just becouse it uses less resources.
In the case of PHP, it tokenizes =+ to T_PLUS_EQUAL, also best executed by PHP executable, and in the other hand, the sum, well, it is tokenized (and executed) just like a sum.

Following the "dumps" from both token_get_all()

<?php echo '<pre>';

print_r(array_map(function($t){if(is_array($t)) $t[0]=token_name($t[0]); return $t;},
          token_get_all('<?php $a=$a+3 ?>')));

print_r(array_map(function($t){if(is_array($t)) $t[0]=token_name($t[0]); return $t;},
          token_get_all('<?php $a+=3 ?>')));

// results in:
?>

Array
(
    [0] => Array
        (
            [0] => T_OPEN_TAG
            [1] =>  1
        )

    [1] => Array
        (
            [0] => T_VARIABLE
            [1] => $a
            [2] => 1
        )

    [2] => =
    [3] => Array
        (
            [0] => T_VARIABLE
            [1] => $a
            [2] => 1
        )

    [4] => +
    [5] => Array
        (
            [0] => T_LNUMBER
            [1] => 3
            [2] => 1
        )

    [6] => Array
        (
            [0] => T_WHITESPACE
            [1] =>  
            [2] => 1
        )

    [7] => Array
        (
            [0] => T_CLOSE_TAG
            [1] => ?>
            [2] => 1
        )

)

Array
(
    [0] => Array
        (
            [0] => T_OPEN_TAG
            [1] =>  1
        )

    [1] => Array
        (
            [0] => T_VARIABLE
            [1] => $a
            [2] => 1
        )

    [2] => Array
        (
            [0] => T_PLUS_EQUAL      /// <= see here!!!!!
            [1] => +=
            [2] => 1
        )

    [3] => Array
        (
            [0] => T_LNUMBER
            [1] => 3
            [2] => 1
        )

    [4] => Array
        (
            [0] => T_WHITESPACE
            [1] =>  
            [2] => 1
        )

    [5] => Array
        (
            [0] => T_CLOSE_TAG
            [1] => ?>
            [2] => 1
        )

)
Gwenore answered 5/7, 2013 at 17:21 Comment(0)
R
0

$a = $a + 3 might use a temporary variable in the PHP VM. While $a += 3 might execute directly.

PS: I emphasize the might. Very not sure...

It might be similar to C++'s: ++i vs. i++ :)

Roxieroxine answered 5/7, 2013 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.