C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

why prefix increment is faster than postfix increment?
Goto page 1, 2, 3, 4, 5, 6, 7  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
jrefactors@hotmail.com
Guest





PostPosted: Sat Oct 22, 2005 8:01 am    Post subject: why prefix increment is faster than postfix increment? Reply with quote



I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are
i = i+1.

i++
++i

Please advise. thanks!!

Back to top
Jonathan Mcdougall
Guest





PostPosted: Sat Oct 22, 2005 8:08 am    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote



[email]jrefactors (AT) hotmail (DOT) com[/email] wrote:
Quote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are
i = i+1.

i++
++i

http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15

And while you're there, read the whole faq.


Jonathan


Back to top
John Carson
Guest





PostPosted: Sat Oct 22, 2005 8:13 am    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote



<jrefactors (AT) hotmail (DOT) com> wrote

Quote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are
i = i+1.

They are obviously not both the same. The prefix form returns the value of
the variable after the incrementation and the postfix form returns the value
of the variable before the incrementation.

For the postfix form, the simple way to implement it is for the compiler to
produce code that creates a temporary that stores the original value and
then to return this value. For a built-in type, this is a cheap operation.
For a class object, it may be an expensive operation.

--
John Carson


Back to top
Christian Bau
Guest





PostPosted: Sat Oct 22, 2005 9:19 am    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote

In article <1129968097.386674.50960 (AT) g44g2000cwa (DOT) googlegroups.com>,
[email]jrefactors (AT) hotmail (DOT) com[/email] wrote:

Quote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are
i = i+1.

i++
++i

Please advise. thanks!!

The C answer and the C++ answer for the built-in operators are: Whoever
makes that claim is not only clueless, but is also the type of dangerous
clueless person who thinks they have a clue. Don't take _any_ advice of
them. Ever.

I don't think you are interested in the C++ answer for operator
overloading yet.

Back to top
Dave Rahardja
Guest





PostPosted: Sat Oct 22, 2005 2:25 pm    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote

On 22 Oct 2005 01:01:37 -0700, [email]jrefactors (AT) hotmail (DOT) com[/email] wrote:

Quote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are
i = i+1.

i++
++i

Please advise. thanks!!

When i is a C++ object with the appropriate operators defined, the prefix
version may eliminate the creation of a temporary object.

-dr

Back to top
Kaz Kylheku
Guest





PostPosted: Sat Oct 22, 2005 4:17 pm    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote

[email]jrefactors (AT) hotmail (DOT) com[/email] wrote:
Quote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are
i = i+1.

i++
++i

If you are throwing away the result, there is no semantic difference at
all, because the only difference between ++i and i++ is whether i or i
+ 1 is returned.

If you don't throw away the result, then they are different operators;
you can't substitute one without the other without making compensating
changes in the surrounding program! You then end up with two different
programs that you have to compare as such. If there is a performance
difference between those programs, it's a result of not just changing
the i++ to ++i, or vice versa, but also a consequence of those other
compensating changes, and how your particular compiler and machine
deals with everything as a whole.

Note that in C++ (this is cross-posted to comp.lang.c++), both forms of
the operator can be user-defined. If you are dealing with a choice
between two forms of the user-defined operator, and performance is
critical, you obviously have to take that into consideration!


Back to top
Gordon Burditt
Guest





PostPosted: Sat Oct 22, 2005 5:50 pm    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote

Quote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are

Any claim that X is faster than Y that doesn't specify a specific
compiler and platform is FALSE. Even if Y is "do X 1000000 times".

Gordon L. Burditt

Back to top
Dave Rahardja
Guest





PostPosted: Sun Oct 23, 2005 5:08 am    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote

On Sat, 22 Oct 2005 17:50:01 -0000, [email]gordonb.foi6x (AT) burditt (DOT) org[/email] (Gordon Burditt)
wrote:

Quote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are

Any claim that X is faster than Y that doesn't specify a specific
compiler and platform is FALSE. Even if Y is "do X 1000000 times".

I think the original poster heard one of those "rules of thumb" that aren't
absolutely true or mathematically proven, but is considered a good
approximation of the truth most of the time. I also assume that the OP wanted
to know what the motivation was behind the saying.

-dr

Back to top
Greg
Guest





PostPosted: Sun Oct 23, 2005 6:37 am    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote

[email]jrefactors (AT) hotmail (DOT) com[/email] wrote:
Quote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are
i = i+1.

i++
++i

Please advise. thanks!!

Consider this program:

void PrintElement(const std::vector<int>::iterator& i)
{
std::cout << *i << " ";
}

int main()
{
std::vector
v.push_back(1); v.push_back(2); v.push_back(3);

std::vector<int>::iterator i = v.begin();

while (i != v.end())
PrintElement( i++ );

while (i != v.begin())
PrintElement( --i );
}

Which PrintElement() call is like the more efficient one: the one with
the postincremented parameter (i++) or the pre-decremented (--i)
parameter? Or is there no reason to think that there would be a
difference?

In this case, it is likely that first PrintElement call with the
postfix incremented paramter has more overhead than the second, because
the compiler must increment the iterator i before it calls
PrintElement. But when the call to PrintElement is made, the compiler
must pass the value of i (or a reference to a temporary copy of i) that
i had before it was incremented. Therefore the compiler has little
choice but to make a copy of i before incrementing i, so that it has an
iterator with which it can call PrintElement.

The second PrintElement call applies a prefix operator to the paramter;
the compiler can therefore pass i directly to PrintElement, since its
incremented value is the appropriate value to pass to PrintElement.

Of course, the difference is not likely to be great, but there is
nonetheless a basis for expecting postfix operators to be less
efficient than prefix operators, especially when applied to parameters
in a function call.

Greg


Back to top
Martin Ambuhl
Guest





PostPosted: Sun Oct 23, 2005 7:20 am    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote

Greg wrote:

Quote:
Consider this program:

void PrintElement(const std::vector<int>::iterator& i)
{
std::cout << *i << " ";
}
[etc.]


When you respond to posts which are crossposted to and <news:comp.lang.c++>, try to give answers that are acceptable in
both. You have posted a bunch of compilation errors to
<news:comp.lang.c>. There is no need to consider your code at all.

Back to top
Greg
Guest





PostPosted: Sun Oct 23, 2005 8:12 am    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote


Gordon Burditt wrote:
Quote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are

Any claim that X is faster than Y that doesn't specify a specific
compiler and platform is FALSE. Even if Y is "do X 1000000 times".

Not necessarily. If one can show that Y performs every operation that X
performs, and then has to perform additional operations outside of that
set and that require a measurable amount of time to complete, then one
would have successfully proven that X is faster than Y. And in fact
that is the case here: the postincrement operator may have to perform
an additional copy operation that the prefix version does not have to
perform. Otherwise the amount of work required of each is the same.

Greg


Back to top
Michael Mair
Guest





PostPosted: Sun Oct 23, 2005 1:57 pm    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote

Greg wrote:
Quote:
Gordon Burditt wrote:

I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are

Any claim that X is faster than Y that doesn't specify a specific
compiler and platform is FALSE. Even if Y is "do X 1000000 times".

Not necessarily. If one can show that Y performs every operation that X
performs, and then has to perform additional operations outside of that
set and that require a measurable amount of time to complete, then one
would have successfully proven that X is faster than Y. And in fact
that is the case here: the postincrement operator may have to perform
an additional copy operation that the prefix version does not have to
perform. Otherwise the amount of work required of each is the same.

Nope. Maybe in source code but not necessarily in the generated code.
The optimisation may have a stage which looks for certain patterns;
it is very well possible that the seemingly slower code qualifies for
the optimisation but the "faster" code does not. This may be the
compiler's fault or yours, depending on the problem at hand.

So, you are right most of the time but not always. However, C++ coding
guidelines often bring the ++()/()++ including the caveats as an
example of "avoiding premature pessimization" and rightly so.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

Back to top
Gordon Burditt
Guest





PostPosted: Sun Oct 23, 2005 4:10 pm    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote

Quote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are

Any claim that X is faster than Y that doesn't specify a specific
compiler and platform is FALSE. Even if Y is "do X 1000000 times".

Not necessarily. If one can show that Y performs every operation that X
performs, and then has to perform additional operations outside of that
set and that require a measurable amount of time to complete, then one
would have successfully proven that X is faster than Y.

Not unless you can prove that the compiler generates the code that
way, and in order to do that, you have to choose a specific compiler.
You cannot test *ALL* compilers, including ones written between
your test and publishing the results. And you can't prove, for
example, that the compiler doesn't add a time-wasting loop to X but
not to Y, without referring to a specific compiler.

There are some situations where doing more can be faster.
For example repeating this statement 1000000 times could be
faster than doing it 10 times:
unsigned int x;

x = x << 1;

since if the width of x is less than 1000000 bits, this results in
a constant independent of the original value of x, and the compiler
might realize this, but if the width of x is greater than 10, and it
must be, it has to shift x.

Quote:
And in fact
that is the case here: the postincrement operator may have to perform
an additional copy operation that the prefix version does not have to
perform. Otherwise the amount of work required of each is the same.

You're assuming things about the underlying instruction set that
may not be true, and assuming that the compiler doesn't do a
poor job of generating code for one and a good job for the other.
The effect of a cache hit/miss can also do funny things to code
that looks like it should run in the same time as other code.

Gordon L. Burditt

Back to top
Christian Bau
Guest





PostPosted: Sun Oct 23, 2005 4:44 pm    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote

In article <1130055121.143006.220940 (AT) f14g2000cwb (DOT) googlegroups.com>,
"Greg" <greghe (AT) pacbell (DOT) net> wrote:

Quote:
Gordon Burditt wrote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are

Any claim that X is faster than Y that doesn't specify a specific
compiler and platform is FALSE. Even if Y is "do X 1000000 times".

Not necessarily. If one can show that Y performs every operation that X
performs, and then has to perform additional operations outside of that
set and that require a measurable amount of time to complete, then one
would have successfully proven that X is faster than Y.

One would have proven no such thing.

Consider this:

double x, y;
memcpy (&x, &y, sizeof (x) - 1);
memcpy (&x, &y, sizeof (x));

A good compiler will translate the second memcpy to a simple assignment
of a double variable, for example:

Load double y into register
Store register into double x.

In fact, if this is the only time the address of x and y is taken, it is
quite possible that x and y are still kept in floating-point registers,
in which case this is a very fast register-to-register assignment.

On the other hand, the first memcpy will have a much more difficult
implementation, even though it copies one byte less. Not only will it
produce much more code, on most current processors that code will
execute considerably slower.

Back to top
Jack Klein
Guest





PostPosted: Sun Oct 23, 2005 8:01 pm    Post subject: Re: why prefix increment is faster than postfix increment? Reply with quote

On 22 Oct 2005 01:01:37 -0700, [email]jrefactors (AT) hotmail (DOT) com[/email] wrote in
comp.lang.c:

Quote:
I heard people saying prefix increment is faster than postfix
incerement, but I don't know what's the difference. They both are
i = i+1.

What people are these? What are their credentials so that you, or we,
should place any confidence in their opinions?

Quote:
i++
++i

Please advise. thanks!!

One possible item of advice would be for you to associate with
different people.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2, 3, 4, 5, 6, 7  Next
Page 1 of 7

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.