 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Seungbeom Kim Guest
|
Posted: Wed May 10, 2006 1:21 pm Post subject: Postfix is slower than postfix: how likely? |
|
|
It has been said for a long time that we should prefer the prefix
increment/decrement operator to the postfix because the former is never
slower and sometimes it can be faster; e.g.:
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15
Now I wonder: how likely are the cases where you suffer significant
performance loss because you use postfix instead of prefix? Is there
any benchmark that tells postfix can be measurably slower than prefix?
I know I can build a class that takes arbitrarily long to copy to show
that postfix can be slower, but it's not usual to have increment
operators for such classes (especially in loops, where we worry about
performances most). Most of the time we use the operators for built-in
types and iterators, both of which are (supposed to be) cheap to copy.
Moreover, contrary to my expectation, testing with g++-3.4.4 -S -O
revealed no difference at all in the assembly outputs, between
for (container::iterator i = c.begin(); i != c.end(); ++i)
and
for (container::iterator i = c.begin(); i != c.end(); i++)
for any container type in the standard library.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Tomás Guest
|
Posted: Wed May 10, 2006 10:21 pm Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Seungbeom Kim posted:
| Quote: | Now I wonder: how likely are the cases where you suffer significant
performance loss because you use postfix instead of prefix? Is there
any benchmark that tells postfix can be measurably slower than prefix?
|
If we're talking about primitive, built-in types, there's no difference.
If we're talking about user-defined types, then it depends entirely on the
class's definition of the operators. It's commonly said that the prefix is
faster than the postfix, and that's because they're commonly implemented
akin to the following:
class SomeClass {
int i;
public:
SomeClass(int const a) : i(a) {}
SomeClass& operator++() /* This is prefix */
{
++i;
return *this;
}
SomeClass operator++(int) /* This is postfix */
{
SomeClass temp(*this);
/* Involves copy-construction of a temporary */
++(*this);
return temp;
}
};
Note also the the prefix returns by reference, while the postfix returns by
value.
This discussion has been done to death.
-Tomás
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Jeffrey Schwab Guest
|
Posted: Wed May 10, 2006 10:21 pm Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Seungbeom Kim wrote:
| Quote: | It has been said for a long time that we should prefer the prefix
increment/decrement operator to the postfix because the former is never
slower and sometimes it can be faster; e.g.:
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15
Now I wonder: how likely are the cases where you suffer significant
performance loss because you use postfix instead of prefix? Is there
any benchmark that tells postfix can be measurably slower than prefix?
|
Do whatever makes the code clearest. If a post-increment really would
be that slow for a given type, the implementor probably should not
provide the operation at all, or at least should give you some warning
in the documentation.
When in doubt, you may as well err on the side of pre-increment in case
you ever want to switch to a heavier-weight iterator type. I personally
find pre-inc more intuitive, anyway.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Wed May 10, 2006 10:21 pm Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Seungbeom Kim wrote:
Yes, the new style of talking about coding guidelines is to use the
weasel-word "prefer", which avoids saying anything concrete.
| Quote: |
Now I wonder: how likely are the cases where you suffer significant
performance loss because you use postfix instead of prefix? Is there
any benchmark that tells postfix can be measurably slower than prefix?
|
Sure, you can write a benchmark that tests this. You might even find a
measurable difference in the time it takes to run an empty loop. As you
say in the following part (which I snipped), that in real-world
applications it usually doesn't make any noticeable difference. This is
mostly a hypothetical benefit. On the other, hand, if an application
runs too slowly, it may be because there are a bunch of little things
that are done sub-optimally (and you won't see that with a profiler), so
it's a good habit to use hypothetically faster techniques when they
don't have any downside. Since ++i and i++ in this context convey the
same meaning, there's no technical reason to choose one over the other,
so general performance considerations suggest using ++i.
--
Pete Becker
Roundhouse Consulting, Ltd.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Thu May 11, 2006 1:21 pm Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Jeffrey Schwab wrote:
| Quote: | Seungbeom Kim wrote:
It has been said for a long time that we should prefer the prefix
increment/decrement operator to the postfix because the former is never
slower and sometimes it can be faster; e.g.:
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15
Now I wonder: how likely are the cases where you suffer significant
performance loss because you use postfix instead of prefix? Is there
any benchmark that tells postfix can be measurably slower than prefix?
Do whatever makes the code clearest.
|
I don't think one of '++i' or 'i++' makes the code clearer than the
other; at least in those cases I am mostly concerned about (where the
return values are ignored).
| Quote: | When in doubt, you may as well err on the side of pre-increment in case
you ever want to switch to a heavier-weight iterator type. I personally
find pre-inc more intuitive, anyway.
|
I'm already on that side. The question is how often I gain from it,
when even something like std::set<T>::iterator shows no difference
at all and I cannot think of anything more complex in common use.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Thu May 11, 2006 1:21 pm Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Seungbeom Kim wrote:
| Quote: | It has been said for a long time that we should prefer the prefix
increment/decrement operator to the postfix because the former is never
slower and sometimes it can be faster; e.g.:
|
IMO, the main point in using languages like C++ and C is to use no more
than needed and to communicate the intention of code clearly. Among
other things needless use of postfix increment defeats these goals.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
rolkA Guest
|
Posted: Thu May 11, 2006 1:21 pm Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Seungbeom Kim <musiphil (AT) bawi (DOT) org> wrote:
| Quote: | It has been said for a long time that we should prefer the
prefix increment/decrement operator to the postfix because the
former is never slower and sometimes it can be faster; e.g.:
http://www.parashift.com/c++-faq-lite/operator-
overloading.html#f
aq-13.15
Now I wonder: how likely are the cases where you suffer
significant performance loss because you use postfix instead of
prefix? Is there any benchmark that tells postfix can be
measurably slower than prefix?
I know I can build a class that takes arbitrarily long to copy
to show that postfix can be slower, but it's not usual to have
increment operators for such classes (especially in loops, where
we worry about performances most). Most of the time we use the
operators for built-in types and iterators, both of which are
(supposed to be) cheap to copy. Moreover, contrary to my
expectation, testing with g++-3.4.4 -S -O revealed no difference
at all in the assembly outputs, between
for (container::iterator i = c.begin(); i != c.end(); ++i)
and
for (container::iterator i = c.begin(); i != c.end(); i++)
for any container type in the standard library.
|
Hi,
To my mind you miss the real point: Why using postfix form if you
don't care about the previous value ?
Be consistant: Prefer using the prefix form, unless you need the
original value, for example:
for (size_t i = v.size(); i-- > 0) // postfix when you need it
but:
for (size_t i = 0; i < v.size(); ++i)
You're right: compilers will optimise i++ in such a loop... But
what's the point in using i++ ? being consistant improve
readability.
--
Sam / rolkA
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Thu May 11, 2006 1:21 pm Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Tomás wrote:
| Quote: | Seungbeom Kim posted:
Now I wonder: how likely are the cases where you suffer significant
performance loss because you use postfix instead of prefix? Is there
any benchmark that tells postfix can be measurably slower than prefix?
If we're talking about primitive, built-in types, there's no difference.
If we're talking about user-defined types, then it depends entirely on the
class's definition of the operators. It's commonly said that the prefix is
faster than the postfix, and that's because [...]
This discussion has been done to death.
|
I might not have made myself clear, but I'm afraid that's not what I
asked; I know why prefix is preferred and I've been advocating it to
other people for quite a long time. But people (especially those in
the habit of writing postfix for no particular reason and reluctant to
change) would ask me if there really was any difference and how likely
it would be (so that they could be convinced to convert, or they would
just stay where they were and continue to write postfix). That's why
and what I asked. Hopefully I made myself clearer this time.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Fri May 12, 2006 12:21 am Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Seungbeom Kim wrote:
| Quote: | Tomás wrote:
Seungbeom Kim posted:
Now I wonder: how likely are the cases where you suffer significant
performance loss because you use postfix instead of prefix? Is there
any benchmark that tells postfix can be measurably slower than prefix?
If we're talking about primitive, built-in types, there's no difference.
If we're talking about user-defined types, then it depends entirely on the
class's definition of the operators. It's commonly said that the prefix is
faster than the postfix, and that's because [...]
This discussion has been done to death.
I might not have made myself clear, but I'm afraid that's not what I
asked; I know why prefix is preferred and I've been advocating it to
other people for quite a long time. But people (especially those in
the habit of writing postfix for no particular reason and reluctant to
change) would ask me if there really was any difference and how likely
it would be (so that they could be convinced to convert, or they would
just stay where they were and continue to write postfix). That's why
and what I asked. Hopefully I made myself clearer this time.
|
It depends on the types involved. For inlined code, there
may well be little or no difference. For types where
copying is heavier, and inlining is not available, the
overhead of postfix may be greater. But this is all
secondary; the best reason to prefer prefix is that it
is the most direct expression of intent. Postfix increment
is "make a copy of X, and increment X", and usually you
just mean "increment X", so why ask for more?
-- James
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Fri May 12, 2006 12:21 am Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Seungbeom Kim wrote:
| Quote: | Jeffrey Schwab wrote:
Seungbeom Kim wrote:
It has been said for a long time that we should prefer the prefix
increment/decrement operator to the postfix because the former is never
slower and sometimes it can be faster; e.g.:
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15
Now I wonder: how likely are the cases where you suffer significant
performance loss because you use postfix instead of prefix? Is there
any benchmark that tells postfix can be measurably slower than prefix?
Do whatever makes the code clearest.
I don't think one of '++i' or 'i++' makes the code clearer than the
other; at least in those cases I am mostly concerned about (where the
return values are ignored).
|
Nonetheless, there are good arguments with writing i++
only where we want its odd semantics -- whenever I see
it in C++ code, I wonder whether
(a) there's some reason why it was needed, and
(b) whether the programmer who write it knows C++ well.
| Quote: | When in doubt, you may as well err on the side of pre-increment in case
you ever want to switch to a heavier-weight iterator type. I personally
find pre-inc more intuitive, anyway.
I'm already on that side. The question is how often I gain from it,
when even something like std::set<T>::iterator shows no difference
at all and I cannot think of anything more complex in common use.
|
If you only ever increment things which are builtins or
whose operator++ implementations are inline, you may well
gain nothing except the (slightly subjective) improvement
in code clarity.
It seems to me to be a waste of time wondering if you do
gain performance from writing ++x instead of x++, given
that you obviously don't lose anything, and you *might*
lose the other way. ++x is in no way worse (except that
in code heavily influenced by C programmers it might not
fit with what they're used to, or with the rest of the
code -- but writing C++ to suit C programmers is not
often an optimal choice).
-- James
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Tomás Guest
|
Posted: Fri May 12, 2006 12:21 am Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Seungbeom Kim posted:
| Quote: | I might not have made myself clear, but I'm afraid that's not what I
asked; I know why prefix is preferred and I've been advocating it to
other people for quite a long time. But people (especially those in
the habit of writing postfix for no particular reason and reluctant to
change) would ask me if there really was any difference and how likely
it would be (so that they could be convinced to convert, or they would
just stay where they were and continue to write postfix). That's why
and what I asked. Hopefully I made myself clearer this time.
|
Very simple. Get a really big, complicated class whose copy-constructor
takes a long time to run. Then time a loop:
for( ExpensiveClass i; i < 999; ++i) ;
Versus:
for( ExpensiveClass i; i < 999; i++) ;
If they really need that much convincing, they won't be Grade A
programmers.
(Also turn off all compiler optimizations for good measure).
-Tomás
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Fri May 12, 2006 12:21 pm Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
kanze wrote:
| Quote: | Tomás wrote:
Seungbeom Kim posted:
Now I wonder: how likely are the cases where you suffer
significant performance loss because you use postfix instead
of prefix? Is there any benchmark that tells postfix can be
measurably slower than prefix?
If we're talking about primitive, built-in types, there's no
difference.
It depends on the compiler. And the machine -- in the C world,
postfix became preferred because on the earliest C compilers for
the PDP-11, it was faster than prefix. For ++; the reverse was
true for --, but since -- was considerably rarer...
If we're talking about user-defined types, then it depends
entirely on the class's definition of the operators.
And the compiler, and the machine.
|
Since the prefix operator "increments" and the postfix operator
"copies, then increments" a value, it should be clear that the postfix
operator will never be faster than the prefix operator and is much more
likely to be slower.
| Quote: | It's commonly said that the prefix is faster than the postfix,
It's also been pointed out often enough that actual measurements
don't support what is commonly said. That this is more a myth
than anything else.
|
Unless the compiler can manage to optimize away the extra copy
operation required when postfix-incrementing a value,
prefix-incrementation is certain to be faster.
| Quote: | [...]
This discussion has been done to death.
Never the less, Seungbeom is the first beside myself to post
actual facts, as opposed to speculation. To date, no one has
actually shown a real life iterator where it made a measurable
difference.
|
The speed difference between prefix and postfix incrementing an
iterator can readily be measured with, say, a std::vector of
std::string's:
#include <iostream>
#include <vector>
#include <string>
using std::vector;
using std::string;
void Output(std::vector<std::string>::const_iterator iter);
int main()
{
vector<string> v;
v.resize(10000, "abc");
vector<string>::const_iterator iter = v.begin();
do {
Output( ++iter);
} while( iter != v.end());
}
void Output(std::vector<string>::const_iterator iter)
{
std::cout << *iter;
}
The user and system time needed to run this program are:
user 0m0.009s
sys 0m0.007s
Replacing the do loop with this while loop:
while (iter != v.end())
{
Output( iter++);
}
takes measurably more time to complete:
user 0m0.016s
sys 0m0.026s
In other words, due to the extra copying required, the
postfix-incrementing iteration is on the order of 2.5 times as slow as
the same iteration performed with a prefix-incrementing iterator. A
difference in time that should not be at all surprising.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Fri May 12, 2006 12:21 pm Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Tomás wrote:
| Quote: | Seungbeom Kim posted:
I might not have made myself clear, but I'm afraid that's
not what I asked; I know why prefix is preferred and I've
been advocating it to other people for quite a long time.
But people (especially those in the habit of writing postfix
for no particular reason and reluctant to change) would ask
me if there really was any difference and how likely it
would be (so that they could be convinced to convert, or
they would just stay where they were and continue to write
postfix). That's why and what I asked. Hopefully I made
myself clearer this time.
Very simple. Get a really big, complicated class whose
copy-constructor takes a long time to run.
|
Now you're being silly. Such classes can't be used as
iterators. That's all there is to it. The STL copies iterators
right and left -- any iterator which is expensive to copy will
give unacceptable performance with the STL.
| Quote: | Then time a loop:
for( ExpensiveClass i; i < 999; ++i) ;
Versus:
for( ExpensiveClass i; i < 999; i++) ;
If they really need that much convincing, they won't be Grade
A programmers.
|
If they pay any attention to such artificial measures, they
aren't very professional. The question that Seungbeom asked was
very concrete -- what is the probability of it actually making a
difference in a real program? Which is the only question which
would really concern a professional programmer.
| Quote: | (Also turn off all compiler optimizations for good measure).
|
Let's see -- we're worried about performance, so we turn off all
compiler optimizations. I've never worked in an organization
that worked like that. (We generally deliver without any
optimizations, because we don't need the speed, and compilers
are generally more reliable with optimization turned off. But
when performance is an issue, compiler optimizations are
generally more reliable than manual optimizations.)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Fri May 12, 2006 12:21 pm Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
James Dennett wrote:
| Quote: | Postfix increment
is "make a copy of X, and increment X", and usually you
just mean "increment X", so why ask for more?
|
Because that's what people did before you, in your existing
code.
Because there is a long history for preferring postfix, dating
back to the PDP-11 and the early C compilers, where postfix ++
was actually faster. (Not by that much, but on a PDP-11,
programs were smaller, and did less, so the difference was more
often visible.)
I think that the usual situation is that older people, like
myself, are in the habit of preferring postfix, because that's
what K&R did. And younger people prefer it because that's what
their mentors used.
All other things being equal, I now prefer prefix. But I don't
find the difference large enough to justify bucking existing
practice.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Sat May 13, 2006 12:21 am Post subject: Re: Postfix is slower than postfix: how likely? |
|
|
Greg Herlihy wrote:
| Quote: | The speed difference between prefix and postfix incrementing an
iterator can readily be measured with, say, a std::vector of
std::string's:
#include <iostream
#include <vector
#include <string
using std::vector;
using std::string;
void Output(std::vector<std::string>::const_iterator iter);
int main()
{
vector<string> v;
v.resize(10000, "abc");
vector<string>::const_iterator iter = v.begin();
do {
Output( ++iter);
} while( iter != v.end());
}
void Output(std::vector<string>::const_iterator iter)
{
std::cout << *iter;
}
The user and system time needed to run this program are:
user 0m0.009s
sys 0m0.007s
Replacing the do loop with this while loop:
while (iter != v.end())
{
Output( iter++);
}
takes measurably more time to complete:
user 0m0.016s
sys 0m0.026s
In other words, due to the extra copying required, the
postfix-incrementing iteration is on the order of 2.5 times as slow as
the same iteration performed with a prefix-incrementing iterator. A
difference in time that should not be at all surprising.
|
Amazing! Copying a vector iterator (perhaps twice, since postfix will
return by value) is over 2.5 times as expensive as constructing a
string and sending it to cout.
I think it is more likely that your timescales were such that the
timings aren't meaningful, or that the undefined behavior in your
prefix version meant that version exited faster.
How long does it take on your system if you replace the loop with
std::cout << "Hello world\n";
???
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
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
|
|