Is x + = 1 more efficient than x = x + 1?

At x = x + 1 , is x executed twice? If so, does this mean in x += 1 , x is evaluated only once? How are two expressions evaluated in terms of compiler middleware?

For example, x++ might mean: select location x , load the contents of x into a register, and increase the value of x in memory.

I also read that x += 1 is useful when x not a simple variable, but an expression that includes an array. Any ideas why this is so?

+10
performance c increment
Sep 19 '11 at 13:41
source share
6 answers

In most compilers, they would be identical.

+12
Sep 19 '11 at 13:44
source share

To give you an example of a "real world", consider this program:

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

Compilation with GCC in Darwin 11 with the following flags:

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

The following program will generate, with the exception of the comments and blank lines that I added. Take a specific look at 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 
+11
Sep 19 '11 at 14:37
source share

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

This is not true.

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

This is not true.




The reason for the preference x += 1 - x = x+1 arises when x is replaced by a much longer identifier or, possibly, a field in the class or structure. In this situation, the version x += 1 more readable and, more importantly, avoids the error of repeating itself.

+7
Sep 19 '11 at 13:53 on
source share

So, there are already some questions that cover what you ask here:

x = x + 1 vs x + = 1

Increment: x ++ vs x + = 1

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

The bottom line is that in most, but not all languages, the compiler will make them the same anyway, so there is no difference in performance. The questions above provide details on this.

+3
Sep 19 '11 at 1:47 april
source share

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.

0
Sep 19 '11 at 13:51
source share

Since recording requires no more than 4 characters, not 5.

This is the only way in which x+=1 "more efficient" than x=x+1 . However, the += operator has several other advantages, for example, the fact that (x)+=1 works in macros, where x is an expression that can have side effects that you want to avoid evaluating more than once ...

0
Sep 19 '11 at 14:11
source share



All Articles