CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
why n++ executes faster than n+1?



Category C Interview Questions
Rating (4.8) By 39 users
Added on 7/19/2006
Views 5399
Rate it!
Answers:

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



The question is meaningless since the two aren't equivalent.

n++ modifies n.
n+1 does not.

If the purpose is to increment n by one and n is a register variable, the two are almost the same in processor resources, except that the result of n+1 requires the compiler to allocate a second register for the intermediate result.

Also, the first answer is incorrect. The 'INC EAX' instruction in an x86 CPU is no faster than an 'ADD EAX,1' instruction and the only benefit is to reduce code space.



 Posted by: Joel Corley    

Contact Joel Corley  Contact Joel Corley

Memory related operations takes more time in m+1 it goes to the memory twice ie to search and to store whereas m++ goes only once



 Posted by: imran    

Contact imran  Contact imran

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



--------------------------------------------------------------------------------

The question is meaningless since the two aren't equivalent.

n++ modifies n.
n+1 does not.



 Posted by: alekh    

Contact alekh  Contact alekh

Well, it does not!
While we might be tempted to say that 'increment' instruction executes faster than a 'sum' instruction, I have not come across any compiler which uses 'sum' instruction for 'k+1'.
Also, this issue is totally dependent upon the instruction set of the machine (and compiler) for which the code is compiled. So, there is no point in making it a general rule.



 Posted by: Nileshkumar    

Contact Nileshkumar  Contact Nileshkumar

n++ is unary operation and n+1 is binary operation.unary operation takes less time than binary operation.that is why n++ is faster than n+1



 Posted by: sereena    

Contact sereena  Contact sereena

n++ requires less memory as compared to n+1 as well as n++ requires just 1 reg bt n+1 requires one more register to store the answer



 Posted by: astha patni    

Contact astha patni  Contact astha patni

hi..
n++
and n+=1 both will give the same results but when you compile these instructions seperately, compliler will generate only INR for n++ and
for n += 1 will generate ADD instruction . So this INR instruction is run little smaller than ADD.



 Posted by: shivanand    

Contact shivanand  Contact shivanand


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
What is the difference between a string and an array?


View Answer
Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?


View Answer
Can the sizeof operator be used to tell the size of an array passed to a function?


View Answer
Is using exit() the same as using return?


View Answer
Is it possible to execute code even after the program exits the main() function?


View Answer
What is a static function?


View Answer
How do you print an address?


View Answer
What is the difference between NULL and NUL?


View Answer
What is the stack?


View Answer
What is a null pointer?


View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/6074/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.67