Which one saves execution time and why i++, ++i, i=i+1 , i+=1.

Questions by rameshwar83   answers by rameshwar83

Showing Answers 1 - 27 of 27 Answers

manoj

  • Feb 16th, 2007
 

i=i+1 is d answer

  Was this answer useful?  Yes

fcawad_03

  • Feb 16th, 2007
 

As i++ and i=i+1 are same and ++i and i+=1 are same ...but i ++ and ++1 take less time for execution because it includes less keyword then i+=1 or i=i+1......Avay

  Was this answer useful?  Yes

umesh_singh

  • Mar 26th, 2007
 

>>As i++ and i=i+1 are same<<
...its okay
 >>and ++i and i+=1 are same >>
i+=1 is nothing but i=i+1;
which is equal to i=i+1;

then how is it equal to ++i
...but i ++ and ++1 take less time for execution because it includes less keyword then i+=1 or i=i+1......Avay

  Was this answer useful?  Yes

baseersd

  • Jul 24th, 2007
 

i++ and ++i both saves the time because both take single machine cycle to execute. Where as i=i+1 and i+=1 takes 2 machine cycles.

  Was this answer useful?  Yes

rajkumar_ks

  • Jun 25th, 2009
 

++i executes faster, since both i++ and ++i takes one cycle. i++ creates a temporary variable and then put the incremented value in it but ++i directly increments the value.

  Was this answer useful?  Yes

The only way to know for sure is to code up each version and measure their performance, or examine the generated machine code.  Never blindly assume that any one form is going to be faster than the others. 

As standalone statements, "i++;", "++i;", "i+=1;", and "i=i+1;" are all equivalent, and a reasonably smart compiler should generate the same machine code for all of them (gcc does). 

Code for correctness first, then readability.  Then if you have any performance concerns, run your code through a profiler to find the real bottlenecks and fix them. 

  Was this answer useful?  Yes

Shivaraj Dalawai

  • Oct 13th, 2015
 

i=i+1:
To perform this operation compiler has to perform two operations:
(1)ADD \i+1
(2)Assignment operation i=x
i++:
To perform this operation compiler has to perform only one operation:
(1)INR
Note: above operation are microprocessor instructions i.e. in assembly language
so i++ is faster.

  Was this answer useful?  Yes

Ashish bhardwaj

  • Jun 29th, 2016
 

In i++ process in goes direct in register but in other (i=i+1 , i+=1) process goes in memory t in i++ first process in goes direct in register but in other (i=i+1 , i+=1) process goes in memory then register thats why i++ is fastest then register thats why i++ is fastest

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions