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

Asked

Viewed 180 times

3

It is true that ++$variavel is faster than $variavel++?

In that reply 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 must change from post to pre-increment when you has the opportunity ...

Are they demonstrably faster? Why?

If pré-incremento really is faster, there’s some reason why I worry about using it instead of pós-incremento in the codes I’ve used? I mean, this difference in performance makes a lot of difference in the application?

  • 2

    Interesting...10% is no small thing huh

  • Is 10% in which cases? which code showed this result?

  • @rray, it’s those kinds of things like, "I’ve heard that this is better than that," and it’s in that question that I saw at SOEN

1 answer

4

Editing

I don’t know with PHP, but with C++ (which PHP has as inspiration), the only difference is found when referring to a class. That reply from Soen explains the difference between ++v and v++. In short:

Since 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 construction company is costly in terms of performance, then it can have a significant impact on performance.

The original in english:

Since the Compiler isn’t generating code, but just Calling an Operator++ Function, there is no way to optimize away the tmp variable and its Associated copy constructor. If the copy constructor is Expensive, then this can have a significant performance Impact.

Original response

The only reason I’d have to use the $variavel++ is when it is important for your application - that is, if you need the value to be increased after have read it, then it will be necessary.

That reply from @Maniero explains very well the differences between the ++$v and $v++, and when/how to use each (and when makes a difference).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.