From 8354885584193779288
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,2e93f66caf45e850
X-Google-Attributes: gidf78e5,public
From: Valentin Bonnard <bonnardv@pratique.fr>
Subject: Re: foo++ vs. ++foo (was Re: var::x)
Date: 1998/04/29
Message-ID: <35473903.167EB0E7@pratique.fr>#1/1
X-Deja-AN: 348754149
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
Content-Transfer-Encoding: 7bit
References: <6hac24$8lb$2@news2.isdnet.net> <6hd9vn$dau$1@camel15.mindspring.com> <Pine.LNX.3.96.980423232203.8843A-100000@earth.terran.org> <354185A9.AB4@pratique.fr> <35461308.52A9@noSPAM.central.beasys.com>
X-Original-Date: Wed, 29 Apr 1998 16:28:19 +0200
Content-Type: text/plain; charset=us-ascii
Organization: Ecole Normale Superieure, Paris, France
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBEAgUANUc5xuEDnX0m9pzZAQHzbAF0CBTj8gOqsa4C3CY59q4mvOqo8MqxRDG7 OOKmjsX+Y8SNAsps6EBBKrwfyxKRj5k= =pfD/
Mime-Version: 1.0
Newsgroups: comp.std.c++


David R Tribble wrote:
> 
> On 20 Apr 1998, Chuck McCorvey wrote:
> >>> Also, notice that I used "++i" rather than "i++" in the macro.
> >>> This can make a significant performance difference as the former
> >>> avoids the necessity of creating a temporary.
> 
> B. James Phillippe wrote:
> >> Really?  How so?  I have never heard this before, and I generally
> >> use foo++ over ++foo (in situations where it doesn't matter).
> 
> Valentin Bonnard wrote:
> > ++x increment x and is the x lvalue; x++ increment x and
> > returns the rvalue of x before the increment; so a copy of
> > x has to be made:
>  [bobbit]
> 
> No, Phillippe is correct.  The compiler can optimize almost anything,

I have explainned in details how and when the compiler 
can optimise.

> and 'x++' and '++x' will probably be translated into identical
> code in most cases. 

For built-ins, yes (they are a particular case of PODs).

> the argument that 'p' is more efficient than 'p != NULL' since
> the latter requires a comparison; it's a bogus argument.

;-)

I like this one.

> > But in general, if the appropriate functions aren't inline,
> > or the compiler doesn't optimise well, it's better to use
> > prefix ++, even on an iterator.
> 
> No.  IT'S BETTER TO WRITE READABLE CODE. 

Who said the contrary ?

Do you think that postfix increment in the middle of an 
expression enhance readabillity ? Are there any common 
cases where ++it isn't readable but it++ is ?

> Leave the optimizing to the compiler, that's it's job,
> and it's better at it than you. 

The compiler is (on average) stupid; programmers are (on 
average) intelligents. The compiler kmows the machine well; 
I don't know it. So I let the compiler optimise the low-level 
details for me, but I will help him (when it's possible).

> I'll wager than 98% of your
> increments account for less than 2% of the execution time of your
> program, so making them "faster" is a waste of time and just adds
> to the confusion of the poor guy who has to maintain your code.

I would say that it can make a difference for programs using 
the STL a lot.

-- 

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



