86

I was reading Stroustrup's "The C++ Programming Language", where he says that out of two ways to add something to a variable

x = x + a;

and

x += a;

He prefers += because it is most likely better implemented. I think he means that it works faster too.
But does it really? If it depends on the compiler and other things, how do I check?

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Chiffa
  • 1,486
  • 2
  • 19
  • 38
  • 47
    "The C++ Programming Language" was first published in 1985. The most recent version was published in 1997, and a special edition of the 1997 version was published in 2000. As a consequence, some parts are hugely out of date. – JoeG Sep 18 '12 at 14:54
  • 5
    The two lines could potentially do something completely different. You have to be more specific. – Kerrek SB Sep 18 '12 at 14:54
  • 1
    I think he means that it works *at least as fast*, which is not the same as *faster*. In a similar way, many prefer `++i` over `i++` because it is likely *at least as fast*. – Bo Persson Sep 18 '12 at 15:04
  • 9
    Related: [Incrementing: x++ vs x += 1](http://stackoverflow.com/q/6509707/922184), [Is x += 1 more efficient than x = x + 1?](http://stackoverflow.com/q/7471891/922184) – Mysticial Sep 18 '12 at 16:38
  • 27
    Modern compilers are smart enough for these questions to be considered 'outdated'. – gd1 Sep 18 '12 at 19:08
  • 1
    @gd1 as the accepted answer points out operator overloading makes that assumption dangerous. – josefx Sep 18 '12 at 21:01
  • 1
    @josefx: You are right. However, leaving out nitpicking, it's pretty obvious we're talking about number arithmetics. He is asking "what is faster?" and clearly he is concerned with the translation of that integer (or floating point) operation in machine code. He is not even thinking about operator overloading. – gd1 Sep 18 '12 at 21:52
  • If anyone wants this merged with the dupe then you'll need to update the answers to change `x=x+a;` => `x = x + 1;` and `x+=a;` => `x += 1;`. Otherwise these answers will look out of place. – Kev Sep 19 '12 at 18:29
  • @gd1 if the question where tagged c instead of c++ i would agree. However it is not and Chiffa does not mention any types, so an answer ignoring operator overloading is at least misleading. I prefer being pedantic over being wrong, which is also why I disagree with it being an exact duplicate of a question targeting `c` only. – josefx Sep 20 '12 at 07:43
  • OMG How did this get that many upvotes? There are toooooons of duplicates out there...... – marco-fiset Sep 21 '12 at 13:08
  • @marco-fiset and yet nobody could be bothered to actually find a duplicate that covers c++ instead they point to a completely different language (c) that lacks several relevant features covered by the accepted answer (operator overloading). – josefx Sep 24 '12 at 11:57
  • 2
    Re-opened this because the duplicate question asks about C not C++. – Kev Apr 01 '13 at 22:19

11 Answers11

216

Any compiler worth its salt will generate exactly the same machine-language sequence for both constructs for any built-in type (int, float, etc) as long as the statement really is as simple as x = x + a; and optimization is enabled. (Notably, GCC's -O0, which is the default mode, performs anti-optimizations, such as inserting completely unnecessary stores to memory, in order to ensure that debuggers can always find variable values.)

If the statement is more complicated, though, they might be different. Suppose f is a function that returns a pointer, then

*f() += a;

calls f only once, whereas

*f() = *f() + a;

calls it twice. If f has side effects, one of the two will be wrong (probably the latter). Even if f doesn't have side effects, the compiler may not be able to eliminate the second call, so the latter may indeed be slower.

And since we're talking about C++ here, the situation is entirely different for class types that overload operator+ and operator+=. If x is such a type, then -- before optimization -- x += a translates to

x.operator+=(a);

whereas x = x + a translates to

auto TEMP(x.operator+(a));
x.operator=(TEMP);

Now, if the class is properly written and the compiler's optimizer is good enough, both will wind up generating the same machine language, but it's not a sure thing like it is for built-in types. This is probably what Stroustrup is thinking of when he encourages use of +=.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 14
    There is also another aspect - _readability_. C++ idiom for adding `expr` to `var` is `var+=expr` and writing it the other way will confuse readers. – Tadeusz Kopec for Ukraine Sep 18 '12 at 15:13
  • This. If you implemented the `+=` operator in a no-AST compiler as in [Crenshaw's tutorial](http://compilers.iecc.com/crenshaw/) it would have a pretty good chance of being faster then the written out version. But all that proves is that you've written an incredibly primitive compiler (even then if you implemented the peephole optimize that Crenshaw suggests you'd probably catch and fix it). – dmckee --- ex-moderator kitten Sep 18 '12 at 20:02
  • 21
    If you find yourself writing `*f() = *f() + a;` you might want to take a good hard look at what you're really trying to achieve... – Adam Davis Sep 18 '12 at 21:31
  • @AdamDavis, ha! so true. Hypothetically you might encounter that situation... but realistically, you're either code golfing or there is a cleaner way to implement that code 99/100 times. – coder543 Sep 19 '12 at 02:00
  • 3
    And if var=var+expr confuses you, but var+=expr does not, you're the oddest software engineer I've ever met. Either are readable; just make sure you're consistent (and we all use op=, so its moot anyway =P) – WhozCraig Sep 19 '12 at 05:35
  • 7
    @PiotrDobrogost: What's wrong with answering questions? In any case, it should be the questioner who checked for duplicates. – Gorpik Sep 19 '12 at 08:18
  • 1
    well, i didn't check specifically. I just typed the question in, and checked for a match. – Chiffa Sep 19 '12 at 09:14
  • 2
    @Gorpik Closing as duplicate also gives answer plus you don't have multiple questions asking the same with no way to easily navigate among them. As to who should check for duplicates; do you really think user with rep of 287 should be more aware of how the site works than user with rep of 24k? – Piotr Dobrogost Sep 19 '12 at 09:28
  • @PiotrDobrogost I believe that closing questions as "exact duplicates" does more harm than good, for two reasons. First, they're never *truly* exact duplicates; it's better to answer the question as it was asked than to refer the questioner to something that covers 90% of what they were asking. Second, answering the question again offers the opportunity to write a *better* answer. – zwol Sep 19 '12 at 13:19
  • @PiotrDobrogost Oh, and maybe most important of all, answering the question even if it's a duplicate gives the questioner -- who is probably new to the site -- a better experience. "Closed as exact duplicate" reads as a brush-off. Go away. You can't even think of a new question. – zwol Sep 19 '12 at 13:32
  • 4
    @PiotrDobrogost seems to me like you're a little... jealous... If you want to go around looking for duplicates, go for it. I, for one, prefer answering the questions rather than looking for dupes (unless it's a question I specifically recall having seen before). It can sometimes be faster, and ergo help the one who asked the question faster. Also, note that this isn't even a loop. `1` is a constant, `a` can be a volatile, a user-defined type, or whatever. Completely different. In fact, I don't understand how this even got closed. – Luchian Grigore Sep 19 '12 at 14:20
  • 1
    *Second, answering the question again offers the opportunity to write a better answer* Nonsense - you could (should) as easily write your answer to the duplicated question. If you answer duplicate without looking at existing answers first you're very likely going to reiterate what's been already written. – Piotr Dobrogost Sep 19 '12 at 17:05
  • @LuchianGrigore *I, for one, prefer answering the questions rather than looking for dupes* It's not one **or** the other. Before writing answer one should check for dupes. What's the point in answering already answered question instead of directing everyone to the existing answers? This only creates mess. – Piotr Dobrogost Sep 19 '12 at 17:13
  • 1
    @jalf *I believe that answering questions is better than not answering questions* Me too so I'm glad we agree. Also I think that checking for duplicate before answering is even better than jumping to the answer right away. I guess you would agree with this. – Piotr Dobrogost Sep 19 '12 at 17:15
  • 1
    *First, they're never truly exact duplicates* Not true. From my observation there are many exact duplicates even if formulated differently. *it's better to answer the question as it was asked than to refer the questioner to something that covers 90% of what they were asking* Then write the missing 10% and refer questioner to the already written 90%. – Piotr Dobrogost Sep 19 '12 at 17:26
  • 1
    *"Closed as exact duplicate" reads as a brush-off.* 1. If someone doesn't get that asking duplicates does harm to all users of the site then it's his fault. 2. You can always refer the person to the rules if you want to be more polite. – Piotr Dobrogost Sep 19 '12 at 17:34
  • @PiotrDobrogost Calm down. It is only ones and zeroes. – zwol Sep 19 '12 at 18:19
  • 1
    @PiotrDobrogost Consider taking this discussion to Meta, since it has nothing to do with this question. – Adam Davis Sep 20 '12 at 14:48
57

You can check by looking at the dissasembly, which will be the same.

For basic types, both are equally fast.

This is output generated by a debug build (i.e. no optimizations):

    a += x;
010813BC  mov         eax,dword ptr [a]  
010813BF  add         eax,dword ptr [x]  
010813C2  mov         dword ptr [a],eax  
    a = a + x;
010813C5  mov         eax,dword ptr [a]  
010813C8  add         eax,dword ptr [x]  
010813CB  mov         dword ptr [a],eax  

For user-defined types, where you can overload operator + and operator +=, it depends on their respective implementations.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    Not true in all cases. I have found it can be faster to load a memory address into a register, increment it, and write it back than to increment the memory location directly (not using atomics). I'll try rustle up the code... – James Sep 18 '12 at 14:52
  • How about user defined types? Good compilers *should* generate equivalent assembly, but there is no such guarantee. – mfontanini Sep 18 '12 at 14:53
  • @mfontanini those would be different operators. For user-defined types, the compiler can't generate the same code. I.e. `+=` won't call `+`. – Luchian Grigore Sep 18 '12 at 14:53
  • @LuchianGrigore How do I obtain the assembly from my code? How did you get that? – David G Sep 18 '12 at 14:54
  • To add to the point of @mfontanini, the definition often is in a different translation unit, and LTO is neither commonly used nor are the current implementations (at least the ones I tried out) advanced enough to inline and optimize this much. –  Sep 18 '12 at 14:54
  • @LuchianGrigore by inlining stuff, the code could end up being the same. My point was that you were only addressing integral types. – mfontanini Sep 18 '12 at 14:56
  • @David: If you are using `gcc` or `g++`, you can add a `-S` flag when compiling. – Chris Dargis Sep 18 '12 at 14:57
  • @mfontanini oh yes, never questioned that :) – Luchian Grigore Sep 18 '12 at 14:57
  • 1
    @LuchianGrigore Nope, if `a` is 1 and `x` is `volatile` the compiler could generate `inc DWORD PTR [x]`. This is slow. – James Sep 18 '12 at 14:58
  • @James volatile is a hint to the compiler. Can you back that up with code? – Luchian Grigore Sep 18 '12 at 14:58
  • well, thanks a lot for an asm commentary, I didn't expect it. To clear things up, I meant integers in my questiont. There should be a separate question for user-defined types – Chiffa Sep 18 '12 at 15:00
  • @Chiffa it depends on the IDE. I use MSVS and you can right-click on the code during debugging and select "show dissasembly"... – Luchian Grigore Sep 18 '12 at 15:01
  • So, to sum it up: it most likely wouldn't matter for in-built types, but might be different for user-defined types and overloaded operators, in which case it would be compiler-dependent, right? – Chiffa Sep 18 '12 at 15:06
  • 1
    @Chiffa not compiler-dependent, but developer-dependent. You could implement `operator +` to do nothing, and `operator +=` to compute the 100000th prime number and then return. Of course, that would be a dumb thing to do, but it's possible. – Luchian Grigore Sep 18 '12 at 15:07
  • @LuchianGrigore. I concede, my usecase involved the use of a temporary with a volatile `x`, i.e. `temp = x + 1; x = temp` was faster in my case than `++x`. – James Sep 18 '12 at 15:34
  • 3
    @James: if your program is sensible to the performance difference between `++x` and `temp = x + 1; x = temp;`, then most probably it should be written in assembly rather than c++... – EmirCalabuch Sep 19 '12 at 09:11
  • @EmirCalabuch VS2010 x64 SDK does not allow inline assembly – James Sep 19 '12 at 13:06
10

The difference between x = x + a and x += a is the amount of work the machine has to go through - some compilers may (and usually do) optimize it away, but usually, if we ignore optimization for a while, what happens is that in the former code snippet, the machine has to lookup the value for x twice, while in the latter one, this lookup needs to occur only once.

However, as I mentioned, today most compilers are intelligent enough to analyse the instruction and reduce the resulting machine instructions required.

PS: First answer on Stack Overflow!

Sagar Ahire
  • 187
  • 6
10

Yes! It's quicker to write, quicker to read, and quicker to figure out, for the latter in the case that x might have side effects. So it's overall quicker for the humans. The human time in general costs much more than the computer time, so that must be what you were asking about. Right?

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
8

It really depends on the type of x and a and the implementation of +. For

   T x, a;
   ....
   x = x + a;

the compiler has to create a temporary T to contain the value of x + a whilst it evaluates it, which it can then assign to x. (It can't use x or a as workspace during this operation).

For x += a, it doesn't need a temporary.

For trivial types, there is no difference.

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
7

If you say += you're making life a lot easier for the compiler. In order for the compiler to recognize that x = x+a is the same as x += a, the compiler has to

  • analyze the left hand side (x) to make sure it has no side effects and always refers to the same l-value. For example, it could be z[i], and it has to make sure that both z and i don't change.

  • analyze the right hand side (x+a) and make sure it is a summation, and that the left hand side occurs once and only once on the right hand side, even though it could be transformed, as in z[i] = a + *(z+2*0+i).

If what you mean is to add a to x, the compiler writer appreciates it when you just say what you mean. That way, you're not exercising the part of the compiler that its writer hopes he/she got all the bugs out of, and that doesn't actually make life any easier for you, unless you honestly can't get your head out of Fortran mode.

Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
6

As you've labelled this C++, there is no way to know from the two statements you've posted. You need to know what 'x' is (it's a bit like the answer '42'). If x is a POD, then it's not really going to make much difference. However, if x is a class, there may be overloads for the operator + and operator += methods which could have different behaviours that lead to very different execution times.

Skizz
  • 69,698
  • 10
  • 71
  • 108
5

You're asking the wrong question.

This is unlikely to drive the performance of an app or feature. Even if it were, the way to find out is to profile the code and know how it affects you for certain. Instead of worrying at this level about which is faster, it's far more important to think in terms of clarity, correctness, and readability.

This is especially true when you consider that, even if this is a significant performance factor, compilers evolve over a time. Someone may figure out a new optimization and the right answer today can become wrong tomorrow. It's a classic case of premature optimization.

This isn't to say that performance doesn't matter at all... Just that it's the wrong approach to achieve your perf goals. The right approach is to use profiling tools to learn where your code is actually spending its time, and thus where to focus your efforts.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Granted, but it was meant as a low-level question, not a big picture "When should I consider such a difference" one. – Chiffa Mar 12 '13 at 12:05
  • 2
    The OP's question was totally legitimate, as shown by other's answers (and upvotes). Although we get your point (profile first, etc.) it definitely *is* interesting to know this kind of stuff - are you really going to profile each and every trivial statement you write, profile the outcomes of each decision you take? Even when there are people on SO who already studied, profiled, disassembled the case and have a general answer to provide? –  Feb 17 '15 at 09:57
5

For a concrete example, imagine a simple complex number type:

struct complex {
    double x, y;
    complex(double _x, double _y) : x(_x), y(_y) { }
    complex& operator +=(const complex& b) {
        x += b.x;
        y += b.y;
        return *this;
    }
    complex operator +(const complex& b) {
        complex result(x+b.x, y+b.y);
        return result;
    }
    /* trivial assignment operator */
}

For the a = a + b case, it has to make an extra temporary variable and then copy it.

Random832
  • 37,415
  • 3
  • 44
  • 63
4

I think that should depend on the machine and its architecture. If its architecture allows indirect memory addressing, the compiler writer MIGHT just use this code instead(for optimization):

mov $[y],$ACC

iadd $ACC, $[i] ; i += y. WHICH MIGHT ALSO STORE IT INTO "i"

Whereas, i = i + y might get translated to (without optimization):

mov $[i],$ACC

mov $[y],$B 

iadd $ACC,$B

mov $B,[i]


That said, other complications such as if i is a function returning pointer etc. should also be thought of. Most production level compilers, including GCC, produce the same code for both statements (if they're integers).
nbrooks
  • 18,126
  • 5
  • 54
  • 66
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
2

No, both ways get handeled the same.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130