(General Query) why n++ executes faster than n+1

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
why n++ executes faster than n+1..... or does it realli execute faster?

thanks
Venugopal.B
 
VenuGopal said:
why n++ executes faster than n+1..... or does it realli execute faster?

I assume that you mean to ask:

| If n is an int, does
| n++;
| execute faster than
| n = n + 1;
| ?

The answer to the above is: no. Both statements compile down to the exact
same IL instructions.
 
I have heard interview questions about why would ++n executes faster
than n++?
The prefix operator will always be faster than the postfix. The postfix
version involves a copy and internal call to the prefix. For simple
types the relative cost between the two should be small. For more
complicated objects (e.g. iterating over a vector) the cost may be
larger. The general consensus in C++ literature (More Exceptional C++,
More Effective C++) is that the prefix version should always be
preferred. Doing this you cannot go wrong. In addition, writing ++
instead of ++ costs nothing.
 
I have heard interview questions about why would ++n executes faster
than n++?

I think they would need a new CTO ;)
The prefix operator will always be faster than the postfix.

No, (with standard side-effect perception) the postfix can never be
faster than the prefix. There is a difference.
The postfix
version involves a copy and internal call to the prefix. For simple
types the relative cost between the two should be small.

None. look at the optimized code.
For more
complicated objects (e.g. iterating over a vector) the cost may be
larger.

No, std::vector::(const_)iterator is usually implemented by classes that
compiles and optimizes down to pointers. Look at the optimized code.
The general consensus in C++ literature (More Exceptional C++,
More Effective C++) is that the prefix version should always be
preferred. Doing this you cannot go wrong. In addition, writing ++
instead of ++ costs nothing.

If you "optimize" your programs by writing "++x" instead of "x++" you
are doing something wrong. ++x and x++ have different semantics and you
should choose the one that fits, not the one you think is fastest.


Should you feel tempted to replace the statement "x = x + 1;" with
something else, you should use "++x;" since you don't care about the
side-effect of "x++", which *may* cost you something.

Even *if* you call the x++ operator, and it's implemented using copying
chances are the optimizer will still be able to remove that copying
because the returned object is unused.

In C# there are other reasons to think before using x++, for example x++
on C# defined class'es compiles but still gives you ++x, even if you
only implemented ++x!
 
Inline

I think they would need a new CTO ;)


No, (with standard side-effect perception) the postfix can never be
faster than the prefix. There is a difference.

I said, "The prefix operator will always be faster than the postfix."
You said, "postfix can never be faster than the prefix. There is a
difference."
I totally didn't get this. Aren't we saying the same thing?
None. look at the optimized code.


So if I did
"return ++x; " instead of "return x++;" in my function, you mean to
say that the assembly code generates the same number of instructions?
What about the copy for the postfix.....
No, std::vector::(const_)iterator is usually implemented by classes that
compiles and optimizes down to pointers. Look at the optimized code.

What about the copy for the postfix? And if you custom implemented a
class called Money and had a prefix and postfix operation defined for
it. The implementation of postfix and prefix would be the same code???
If you "optimize" your programs by writing "++x" instead of "x++" you
are doing something wrong. ++x and x++ have different semantics and you
should choose the one that fits, not the one you think is fastest.


Yes, I understand that the semantic is different and it should be used
on a case by case bases.
Should you feel tempted to replace the statement "x = x + 1;" with
something else, you should use "++x;" since you don't care about the
side-effect of "x++", which *may* cost you something.

Even *if* you call the x++ operator, and it's implemented using copying
chances are the optimizer will still be able to remove that copying
because the returned object is unused.

In C# there are other reasons to think before using x++, for example x++
on C# defined class'es compiles but still gives you ++x, even if you
only implemented ++x!


Sorry, I didn't understand the above statement.


I am trying to understand your arguments and trying to make it clear
for my own understanding. Please explain. Thanks!


Sincerely,
Bobby
 
Prefix operation is faster when compared to postfix operation.
Now let us see how both postfix and prefix work.

// postfix
iterator operator++(int)
{
iterator _Tmp = *this;
++*this;
return (_Tmp);
}

// prefix
iterator& operator++()
{
_Ptr = _Acc::_Next(_Ptr);
return (*this);
}
note: one integer dummy variable is used in postfix operation in order to distinguish weather it is a postfix operation or prefix operation. This dummy variable is never used.
(refer prefix / postfix overloading).

http://www.mindfiresolutions.com/Why-Prefix-is-faster-than-Postfix-753.php
 
Back
Top