Is it true that ++ $ variable (pre-increment) is faster than $ variable ++ (post-increment)?

3

Is it true that ++$variavel is faster than $variavel++ ?

In this answer given in SOEN , we see the following excerpt:

  

... however pre-incrementing is almost 10% faster, which means that you   should switch from post to pre-incrementing when you have the   opportunity ...

What do you mean:

  

... however pre-increment is almost 10% faster, which means   that you should switch from powders to pre-increment when you   you have the opportunity ...

Are they proven to be faster? Why?

If pré-incremento is actually faster, is there any reason why I should bother using it, instead of pós-incremento in the codes I've already used? I mean, does this difference in performance make a lot of difference in the application?

    
asked by anonymous 18.08.2015 / 17:00

1 answer

4

Editing

I do not know with PHP, but with C ++ (which PHP is inspired by), the single difference lies when you are referring to a class . This SOen response explains the difference between ++v and v++ . In summary:

  

Because the compiler is not generating code, but simply calling an operator ++ function, there is no way to optimize the temporary variable and the associated copy constructor. If this builder is costly in terms of performance, then it can have a significant impact on performance.

The original in English :

  

Since the compiler is not generating code, but just calling an operator ++ function, there is no way to optimize away the variable tmp and its associated copy constructor. If the copy constructor is expensive, then this can have a significant performance impact.

Original answer

The only reason you should use $variavel++ is when it's important for your application - that is, if you need the value to be incremented after reading it, then it will be necessary.

This @bigown response explains very well the differences between ++$v and $v++ , and when / how to use each (and when makes a difference).

    
18.08.2015 / 17:14