1

Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?

In C++, I see people frequently use ++p in a for loop or elsewhere, where you want to increment a value, but not use the return value. I've heard this is more efficient because p++ returns the value before incrementing, and thus requires a temporary space.

But, it feels that even a very ignorant compiler will pull out that return value as dead code (as long as the increment code is inlined, and so the compiler can see that the return value wasn't necessary).

I'm trying to come up with a good example where, using some sort of iterator, iter++ will actually create the copy (even though the return value of iter++ is not used.

After all, we don't really frequently consider register allocation either when we write code with iterators.

I learned to use p++ simply because that's what the book I learned from did. Is preferring ++p when no return value is used an archaic practice, or simply part of elegant coding? And why is the language not called ++C then?

Community
  • 1
  • 1
user
  • 7,123
  • 7
  • 48
  • 90
  • Depends on what `p` is. Is it an `iterator`? `int`? Usually the compiler will optimize this stuff away anyways. – Marlon Mar 29 '12 at 21:18
  • 1
    Hmm, parsing a 1000000 character string and removing all non alpha character takes 0.0150 seconds on my pc. Is the difference between ++p and p++ really important? – BlackBear Mar 29 '12 at 21:20

2 Answers2

5

p++ is slower only when ++p doesn't do the job - i.e. when you actually use the return value. Otherwise, the compiler will optimize the code to the same thing.

People prefer using ++i instead of i++ because it describes better what you intend to do: increment i, more than increment i and return the old value. Of course, you tend to stick to old habits. If you're used to writing i++, that's ok, unless the coding standards of the company you work for mandate you use ++i.

Chapter 3 of D&E: ``I picked C++ because it was short, had nice interpretations, and wasn't of the form "adjective C."' In C, ++ can, depending on context, be read as "next," "successor," or "increment," though it is always pronounced "plus plus." The name C++ and its runner up ++C are fertile sources for jokes and puns -- almost all of which were known and appreciated before the name was chosen. The name C++ was suggested by Rick Mascitti. It was first used in December of 1983 when it was edited into the final copies of [Stroustrup,1984] and [Stroustrup,1984c].

Chapter 1 of TC++PL: ``The name C++ (pronounced "see plus plus") was coined by Rick Mascitti in the summer of 1983. The name signifies the evolutionary nature of the changes from C; "++" is the C increment operator. The slightly shorter name "C+" is a syntax error; it has also been used as the name of an unrelated language. Connoisseurs of C semantics find C++ inferior to ++C. The language is not called D, because it is an extension of C, and it does not attempt to remedy problems by removing features. For yet another interpretation of the name C++, see the appendix of [Orwell,1949].''

Sauce.

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

Depending on the iterator, creating a copy can be an extremely time consuming task (think about a naive, stack based iterator for a binary search tree). I suppose you realize that though, and that's not your real question :). Anyway, as far as I'm aware, the compiler is not required to optimize i++ to ++i.

As it's not required to optimize it, I would think it's better to err on the side of caution and stick with ++i.

Corbin
  • 33,060
  • 6
  • 68
  • 78