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?

3
  • Duplicates, duplicates and more duplicates: stackoverflow.com/search?q=[c%2B%2B]+i%2B%2B+%2B%2Bi
    – sth
    Commented Jan 7, 2010 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
    Commented Jan 7, 2010 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. Commented Jan 7, 2010 at 12:38

5 Answers 5

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.

4
  • 1
    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.
    – anon
    Commented Jan 7, 2010 at 12:37
  • @Neil -- properly qualified, now.
    – tvanfosson
    Commented Jan 7, 2010 at 12:43
  • 3
    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. Commented Apr 22, 2019 at 1:07
  • 1
    How common is it for compilers to optimize out the difference when the value of that increment expression isn't referenced in any capacity? Commented Jul 9, 2021 at 19:09
15

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.

7
  • 24
    You mean i++ in the second statement there. Commented Jan 7, 2010 at 12:40
  • 1
    No I didn't. Reread my answer. I did however mean "pre-increment" not "pre-incremented". Commented Jan 7, 2010 at 12:54
  • 1
    The statement is correct. He is referring to the value before it is incremented.
    – Marcell
    Commented May 18, 2016 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
    Commented Apr 4, 2019 at 14:31
  • 2
    @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
    Commented Jun 23, 2020 at 18:39
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.

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).

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.

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.
    – Pablo
    Commented Sep 2, 2015 at 18:26
  • 1
    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. Commented Nov 19, 2019 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". Commented Jan 14, 2022 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
    Commented Aug 17, 2022 at 9:26