10

In x = x + 1, is x evaluated twice? If so, does that mean in x += 1, x is only evaluated once? How are the two expressions evaluated in terms of compiler intermediate code?

For example, x++ could mean : take the location of x, load the contents of x into a register, and increment the value of x in memory.

Also I have read that x += 1 is useful when x is not a simple variable, but an expression involving an array. Any ideas why this is the case?

Jeremy Visser
  • 6,237
  • 1
  • 25
  • 30
user7
  • 2,339
  • 5
  • 25
  • 29
  • 11
    This sounds like a lot of unfounded assumptions, in particular the title. I don’t see a reason why one should be more efficient than the other and I’m reasonably confident that it’s not the case on most C compilers. – Konrad Rudolph Sep 19 '11 at 13:44
  • 3
    A compiler worth the money you paid for it will compile both expressions to the same object code. If you are worried about performance: **measure**, measure different code, different compilation flags, different resource usage, different everything. – pmg Sep 19 '11 at 13:45
  • 4
    Do you know that x += 1 is more efficient than x = x + 1? Have you looked at the generated object code? – Joe Sep 19 '11 at 13:45
  • Maybe this was applicable to older compilers..I am curious why one could be more efficient than the other in an older compiler...I read about this in a book called "Deep C Secrets". – user7 Sep 19 '11 at 13:47
  • 4
    How about finding some books that are not 17 years old? – Svish Sep 19 '11 at 13:56
  • There is conceivably a difference in C++ (if `x` is a UDT), but there should be no difference in C. – Oliver Charlesworth Sep 19 '11 at 13:57
  • Ok..so if x is a user defined type, how would x =+1 be more efficient? – user7 Sep 19 '11 at 14:00
  • 1
    Asking about UDTs is a different question. This question is about C. – David Heffernan Sep 19 '11 at 14:00
  • Not only is it a duplicate; this question has a false assumption in the subject line. – R.. GitHub STOP HELPING ICE Sep 19 '11 at 14:12
  • @David: Yes, that was my point... – Oliver Charlesworth Sep 19 '11 at 14:41

6 Answers6

21

Just to give you a "real-world" example, consider this program:

int main()
{
    int i = 0;
    i += 1;
    i++;
    i = i + 1;
    return 0;
}

Compiling it with GCC, in Darwin 11 with the following flags:

  • -S stop in assembler
  • -m32 to 32-bit platform, just to simplify things a bit

Will generate the following program, except for the comments and blank lines which I added. Take a look specially in the comments.

        .section        __TEXT,__text,regular,pure_instructions
        .globl  _main
        .align  4, 0x90
_main:
        pushl   %ebp              # cdecl function stuff
        movl    %esp, %ebp        #
        subl    $12, %esp         # get room for variables    
        movl    $0, -12(%ebp)     # i = 0;

        ; i += 1
        movl    -12(%ebp), %eax   # load i in register a
        addl    $1, %eax          # add 1 to register a
        movl    %eax, -12(%ebp)   # store it back in memory

        ; i++
        movl    -12(%ebp), %eax   #
        addl    $1, %eax          # just the same
        movl    %eax, -12(%ebp)   #

        ; i = i + 1
        movl    -12(%ebp), %eax   #
        addl    $1, %eax          # just the same
        movl    %eax, -12(%ebp)   #

        movl    $0, -8(%ebp)
        movl    -8(%ebp), %eax
        movl    %eax, -4(%ebp)
        movl    -4(%ebp), %eax
        addl    $12, %esp
        popl    %ebp
        ret

.subsections_via_symbols
sidyll
  • 57,726
  • 14
  • 108
  • 151
  • 1
    I don't know why this isn't marked as the answer, since it provides proof and not just an opinion. – nhouser9 Oct 02 '16 at 21:43
  • This should be marked as the answer. Also do we find any difference in other compilers or does it follow the same pattern ? – Sanved Nov 28 '16 at 14:12
  • 2
    @SanVed Thanks. Hard to tell, considering the amount of compilers. But any compiler with a decent development will probably contain this "intelligence". Compilers are not just direct translators from high to low level, there is a whole science involved and examples like this one are among the most basic and first elements of this science. So any modern and major compiler should include it. – sidyll Nov 28 '16 at 22:43
12

In most compilers these would be identical

Ofir
  • 8,194
  • 2
  • 29
  • 44
  • 1
    what about the older compilers? which do not optimize as much as the newer ones..why are they evaluated differently? – user7 Sep 19 '11 at 13:52
  • 1
    If the compiler is inefficient, it may ignore the fact that x is referred to twice and load it and then perform the incrementing operation on the loaded value. – Ofir Sep 19 '11 at 13:53
  • and what about the comment about arrays? – user7 Sep 19 '11 at 13:56
  • 1
    again - a good compiler will be able to produce the same code, but an inefficient one might have more trouble with more complex expressions such as those involving array members – Ofir Sep 19 '11 at 14:00
8

Why is x += 1 more efficient than x = x+1?

It isn't.

Why is x++ more efficient than x += 1?

It isn't.


The reason for preferring x += 1 to x = x+1 comes about when x is replaced with a much longer identifier, or perhaps a field in a class or struct. In that situation, the x += 1 version is more readable and even more importantly avoids the pitfalls of repeating yourself.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
3

So there are already some questions that cover what you're asking here:

x=x+1 vs. x +=1

Incrementing: x++ vs x += 1

Which is faster? ++, += or x + 1?

The bottom line is that in most but not all languages the compiler is going to make them identical anyway, so there's no difference in efficiency. The questions above go into a fair bit of detail on the subject.

Community
  • 1
  • 1
lxt
  • 31,146
  • 5
  • 78
  • 83
0

With gcc, you can get the assembler code with gcc -S foo.c. I tried it with x += 1 and x = x + 1 and it was the same.

wst
  • 1
0

Because it takes 4 characters to write rather than 5.

This is the only way in which x+=1 is "more efficient" than x=x+1. However the += operator does have a few other advantages, like the fact that (x)+=1 works in macros where x is an expression that may have side effects which you want to avoid evaluating more than once...

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711