Re: Postfix is slower than postfix: how likely?
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?
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.
That's what my benchmarks show as well. With a much earlier
version of g++, because I did them some years ago.
My position is that if you are starting a greenfields project,
and you have to choose one for your coding guidelines, you might
as well choose prefix, because it can't hurt. The probability
of it making a difference, however, is so small that it's not
worth changing existing code, or even being inconsistant with it
in new parts -- if you have existing code, stick with whatever
it does (supposing it does something consistently, of course).
--
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! ]