3

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

I understand that:

i++;

Is supposed to take more instructions than

++i;

Because it's generating an intermediate result that you don't need to use, so people put "++i" on lines of code that have no use for the intermediate result.

However, compilers are really smart, and they can tell if a result is being used or not. They can tell all kinds of things. But they aren't magic

So I am curious - on modern compilers does picking one way over the other actually matter or will it simply compile to the same machine code regardless?

Community
  • 1
  • 1
Nektarios
  • 10,173
  • 8
  • 63
  • 93

4 Answers4

9

It does not matter when using it on an int. However, when using iterators or other objects that overload operator++, it might still make a difference.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • He's asking about compiled instructions, etc.; can you back your claim up with generated assembly or some such? – Platinum Azure Feb 16 '11 at 17:12
  • 1
    @Azure: If the operator is overloaded (and especially if the compiler doesn't know the implementation of the overload), it can't decide whether replacing post- with pre-increment makes a difference (in terms of side effects the overloads cause - yes, that would be horrible code, but still...). –  Feb 16 '11 at 17:17
2

This doesn't make difference for native types, but could do some difference with user defined types (ie classes, structs, enums, ...), since the user could overload the operator, and the postfix one (var ++) could be a little slower sometimes.

peoro
  • 25,562
  • 20
  • 98
  • 150
  • Can you overload operators for enums? Wow... –  Feb 16 '11 at 17:12
  • 1
    @Billy: You can overload operators for enum types in C++98 as well. – James McNellis Feb 16 '11 at 17:15
  • That comes particularly handy to overload `operator&` and `operator|` when using enums as flags. -- enum operators can be overloaded also in C++98, but only as non-member, of course. – peoro Feb 16 '11 at 17:17
1

In C, there should be no difference. The compiler is smart enough to optimize the extra copy away if it is not being used. In C++, it can make a huge difference, because if the variable defined by i can be of a type which defines the prefix and postfix versions of operator++ separately, which makes it impossible for the compiler to remove the copy in the general case. Therefore the compiler won't be able to get rid of the copy even if it isn't used.

For consistency's sake, I'd use the prefix form unless you must use the postfix form. (Even though I think the postfix form is "prettier")

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
1

Visual C++ seems to output the same code in each case:

    i++;

00AB149C  mov         eax,dword ptr [i]  
00AB149F  add         eax,1  
00AB14A2  mov         dword ptr [i],eax  

    ++j;

00AB14A5  mov         eax,dword ptr [j]  
00AB14A8  add         eax,1  
00AB14AB  mov         dword ptr [j],eax  
Raynos
  • 166,823
  • 56
  • 351
  • 396
Steve Rowe
  • 19,411
  • 9
  • 51
  • 82
  • This doesn't mean a compiler couldn't optimize more one of the two versions, nor that it will generate always the same code. – peoro Feb 16 '11 at 17:25
  • The question was whether, on modern compilers, there was a difference. Empirically, there is not. – Steve Rowe Feb 16 '11 at 19:44