top button
Flag Notify
Site Registration

Why a++ executes faster than a+1 in C or C++?

+5 votes
1,621 views
Why a++ executes faster than a+1 in C or C++?
posted Nov 19, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+2 votes
 
Best answer

This question very famous ,The same answer i found everywhere that no of instruction is different
But i am unable to proof it ...can any one help ....if i am missing some thing ...
i generated assembly of a c program by disabling optimization and tried to compare it but found for both it is generating same instruction.
small test code name test.c

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

then generated assembly instruction and compared for both
by using this command

gcc -O0 -c -g -Wa,-a,-ad test.c > test.asm

O0 i added to disable optimization

and i got assembly output in test.asm please see the code part for x++ and x+1

 2:test.c        **** int main () 
    3:test.c        **** {
       9                    .loc 1 3 0
      10                    .cfi_startproc
      11 0000 55            pushl   %ebp
      12                .LCFI0:
      13                    .cfi_def_cfa_offset 8
      14                    .cfi_offset 5, -8
      15 0001 89E5          movl    %esp, %ebp
      16                .LCFI1:
      17                    .cfi_def_cfa_register 5
      18 0003 83EC10        subl    $16, %esp
       4:test.c        ****     int a = 0;
      19                    .loc 1 4 0
      20 0006 C745F400      movl    $0, -12(%ebp)
      20      000000
       5:test.c        ****     int x = 0;
      21                    .loc 1 5 0
      22 000d C745F800      movl    $0, -8(%ebp)
      22      000000
       6:test.c        ****     int i = 0;
      23                    .loc 1 6 0
      24 0014 C745FC00      movl    $0, -4(%ebp)
      24      000000
       7:test.c        ****
       8:test.c        ****     i = ++x;    
      25                    .loc 1 8 0
      26 001b 8345F801      addl    $1, -8(%ebp) /* observe here */
      27 001f 8B45F8        movl    -8(%ebp), %eax
      28 0022 8945FC        movl    %eax, -4(%ebp)
       9:test.c        ****
      10:test.c        ****     i = x + 1;
      29                    .loc 1 10 0
      30 0025 8B45F8        movl    -8(%ebp), %eax /* observe here */
      31 0028 83C001        addl    $1, %eax
      32 002b 8945FC        movl    %eax, -4(%ebp)
      11:test.c        ****
      12:test.c        **** }

can you find any difference in these block of instructions

Only 1 difference i found is no of times it access the different register is different.
% ebp and %aeax registers

please help to wheater i left any option with gcc

answer Nov 19, 2013 by Sachidananda Sahu
Thx, please do the same test for x++ not I = x+ 1 and x = x + 1 not I = x + 1.
I tested but no difference for that also ...

8:test.c        ****     x++;    
  25                    .loc 1 8 0
  26 001b 8345F801      addl    $1, -8(%ebp)
   9:test.c        ****
  10:test.c        ****     x = x + 1;
  27                    .loc 1 10 0
  28 001f 8345F801      addl    $1, -8(%ebp)
So seems compiler has improved over time and able to identify x++ and x=x+1 are same.
Let me mark this is a best answer, thanks for putting effort and clarifying to the person whoever has raised this question.
+1 vote

a++ requires a single machine instruction such as INR (increment) to carry out the increment operation whereas, a+1 requires more instructions to carry out this operation.

answer Nov 19, 2013 by Deepankar Dubey
+1 vote

In case of a + 1 a temp variable is used to store "a + 1" then temp is copied to a.
IN case of a++ it update the value at same address.

a = a + 1 will have to take the value of a, add one to it, and then store the result back to a.

++a will be a single assembly instruction.

answer Nov 19, 2013 by Vikas Upadhyay
Similar Questions
+7 votes
#include<stdio.h>

int &fun()
{
   static int x;
   return x;
}   

int main()
{
   fun() = 10;
   printf(" %d ", fun());

   return 0;
}

It is fine with c++ compiler while giving error with c compiler.

+1 vote

I have written a simple program and stored it with .c file,
When i compiled it with g++ it is getting compiled and giving the proper output when i run that,
but when i compile the same using gcc then it is throwing error.

Below is the sample .c file
#include<stdio.h>
struct A{
        private:
        int a;
        public:
        int sum(){
        a=10;
        }
        void print()
        {
        printf("%d\n",a);
        }
        A()
        {
                printf("Constructor\n");
        }
        A(int b)
        {
                printf("Copy Constructor\n");
        }
        ~A()
        {
                printf("Destructor\n");
        }
};
main()
{
        struct A a(10);
        a.sum();
        a.print();
}
0 votes
#define MY_STRUCT
typedef struct{ 
...
... 
} my_struct;

Above code is giving me error (MY_STRUCT) is not working as expected, Looks that some silly mistake, please point out as I have wasted many hours?

Sorry for hiding my identity?

...