Preincrement faster than postincrement in C++ - true? If yes, why is it? [duplicate]
Asked Answered
G

5

46

I heard about that preincrements (++i) are a bit faster than postincrements (i++) in C++. Is that true? And what is the reason for this?

Globular answered 7/1, 2010 at 12:23 Comment(3)
Duplicates, duplicates and more duplicates: stackoverflow.com/search?q=[c%2B%2B]+i%2B%2B+%2B%2BiRambutan
Which hardware platform. The original preincrement and postincrement where hardware features of the PDP-11. Speed was identical because of the way the machine worked. What specific hardware was being used?Pteropod
With todays optimizing compilers, there's no difference (for simple types I mean). The difference is in indicating your intent to anyone that will read your code.Chemar
K
77

Post-increment usually involves keeping a copy of the previous value around and adds a little extra code. Pre-increment simply does it's job and gets out of the way. I typically pre-increment unless the semantics would change and post-increment is actually necessary.

Kostival answered 7/1, 2010 at 12:26 Comment(4)
Whether this is true of the built-in types depends on how you actually use the operator. For the user defined types, it depends on how you implement the operator.Cassette
@Neil -- properly qualified, now.Kostival
in other words: i++ is equivalent to {auto temp(i); increment(i);return temp;}, and ++i is equivalent to {increment(i); return i;} hence that copy is the difference.Impersonal
How common is it for compilers to optimize out the difference when the value of that increment expression isn't referenced in any capacity?Swum
E
14

On any decent compiler, ++i and i++ are identical if the value is not used.

If the value is used, ++i would need a temporary to store the pre-increment value if identical semantics were wanted.

Eddings answered 7/1, 2010 at 12:30 Comment(7)
You mean i++ in the second statement there.Nyala
No I didn't. Reread my answer. I did however mean "pre-increment" not "pre-incremented".Eddings
The statement is correct. He is referring to the value before it is incremented.Mccue
@Richard Pennignton I've read it more than three times and still don't understand why ++i should need a temporary :/. Please explain on example.Gallion
@Mi-La, if you need to emulate the semantics of a = x++; using ++x, then you will need: tmp = x; ++x; a = tmp;Steger
@Steger But who wanted to emulate x++? Normally the "temporary" (or maybe better a copy) is needed for post-increment, since expression x++ returns x, while it changes x value to x+1.Gallion
@Mi-La, the answer used the phrase: "if identical semantics were wanted". If you do want to emulate the same semantics, then you will need the temporary.Steger
E
9

Postincrements have to make a copy of the object to return the unincremented value. For class types, this could be significant but for "int-like" types (incl. pointers), it's probably not.

Eugenol answered 7/1, 2010 at 12:26 Comment(0)
C
8

The difference for plain types types such as int is probably negligible. I would guess the compiler is able to optimize such cases (e.g. turn a postfix into a prefix increment where appropriate). However, for complex types (typically STL iterators the difference might be noticeable). The compiler is not able to switch to prefix in these cases because the operators actually might do totally different things. I recommend reading STL Iterators and Performance for some background info on the performance penalty on using post-increment for STL iterators(short story: it takes twice the time as the pre-increment).

Cause answered 7/1, 2010 at 12:34 Comment(0)
B
1

actually - it depends. Postincrement needs a copy since it preserves the old value. If the type incremented is a complex type (e.g. an iterator) and not a simple type, the preincrement is faster than the postincrement. That is only true of course if you don't need the value before the increment.

Some compilers might even detect that you don't need a postincrement and optimize the copy away - but as always - it's a bad habit to rely on that.

Botanist answered 7/1, 2010 at 12:27 Comment(4)
Not "as always" as much as "in this case". If readability was at stake (as is common when questions on performance are asked), it's bad habit to turn your code into a pile of clever mess. In this case, though, it's pretty much the same.Dissentient
I don't understand why "i++" is more readable than "++i". The only difference between post increments and preincrements is the addition symbol position. Pre increment are used a lot, for instance, in the Linux codebase. A compiler detecting if a variable is being assigned for an optimization is more complex and less readable than one that ignores it, assuming everything else is the same, but people are not complaining about it.Trochanter
I believe it's more "readable", specifically in languages like English, where we put verbs after subjects. For example, "I am going to the store" is the same, in code, as "I is less than 5" or "I gets 5 added to it".Coquito
How can the compiler be able to optimize your application logic? Only you know which value (old/new) makes sense in the context of your code.Halftone

© 2022 - 2024 — McMap. All rights reserved.