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?
-
Duplicates, duplicates and more duplicates: http://stackoverflow.com/search?q=[c%2B%2B]+i%2B%2B+%2B%2Bi – sth Jan 07 '10 at 12:30
-
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? – S.Lott Jan 07 '10 at 12:32
-
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. – Didier Trosset Jan 07 '10 at 12:38
5 Answers
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.

- 524,688
- 99
- 697
- 795
-
1Whether 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. – Jan 07 '10 at 12:37
-
-
3in 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. – ClarHandsome Apr 22 '19 at 01:07
-
1How common is it for compilers to optimize out the difference when the value *of* that increment expression isn't referenced in any capacity? – JamesTheAwesomeDude Jul 09 '21 at 19:09
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.

- 19,673
- 4
- 43
- 72
-
24
-
1No I didn't. Reread my answer. I did however mean "pre-increment" not "pre-incremented". – Richard Pennington Jan 07 '10 at 12:54
-
1The statement is correct. He is referring to the value before it is incremented. – Marcell May 18 '16 at 12:08
-
@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. – Mi-La Apr 04 '19 at 14:31
-
@Mi-La, if you need to emulate the semantics of a = x++; using ++x, then you will need: tmp = x; ++x; a = tmp; – Dangelov Jan 07 '20 at 09:06
-
@Dangelov 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`. – Mi-La Jan 08 '20 at 08:03
-
1@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. – Dangelov Jun 23 '20 at 18:39
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.

- 28,429
- 12
- 61
- 81
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).

- 12,604
- 8
- 62
- 85
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.

- 10,634
- 6
- 46
- 76
-
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. – Pablo Sep 02 '15 at 18:26
-
1I 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. – Pedro Amaral Couto Nov 19 '19 at 18:36
-
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". – Tamir Nadav Jan 14 '22 at 19:10
-
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. – Daniel Aug 17 '22 at 09:26