Why x += 1 ; is more efficient than x = x+1 ?
It is related to how assembler references the memory location accesss .
The ALU uses registers for referencing values and accumulator for storing partial results .
So , for case 1 :
x = x + 1 ;
MOV A , (x) ; // Move the value at memory location x in accumultor A . The value of x is stored in register
ADD A , 1 ; // ADD 1 IN ACCUMULATor
MOV (X) , A ; // Move the value in accumulator at memory location referenced by x
for case 2 :
x += 1
ADD (x) , 1 ; // Add 1 at memory location referenced by x
So , so coz of efficient dereferncing and no partial result generation , short hand artihmatic noations are efficient even though at an abstract view all operations produce same results
It is related to how assembler references the memory location accesss .
The ALU uses registers for referencing values and accumulator for storing partial results .
So , for case 1 :
x = x + 1 ;
MOV A , (x) ; // Move the value at memory location x in accumultor A . The value of x is stored in register
ADD A , 1 ; // ADD 1 IN ACCUMULATor
MOV (X) , A ; // Move the value in accumulator at memory location referenced by x
for case 2 :
x += 1
ADD (x) , 1 ; // Add 1 at memory location referenced by x
So , so coz of efficient dereferncing and no partial result generation , short hand artihmatic noations are efficient even though at an abstract view all operations produce same results
No comments:
Post a Comment