Re: strlen(), K+1: clarification
- From: "Clive D. W. Feather" <clive@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 27 Feb 2008 21:22:05 +0000
In article <47c4392f.765110578@xxxxxxxxxxxxx>, Richard Harter
<cri@xxxxxxxx> writes
This little bit of ecentricity has its points. The computation
of the terminating value is automatically hoisted out of the loop
body. The new terminating condition is a comparison with 0,
usually a more efficient test than a comparison against a
non-zero value.
As a sociological and psychological note, I opine that it is good
practice to preferentially write descent loops rather than ascent
loops. Sometimes one is preferable, sometimes the other, and
often it doesn't matter.
I disagree. Loops should be written using the most natural form to match
the underlying problem, rather than reversing them in the hope of some
slight efficiency.
From experiment, where the compiler can determine the number of loopsbefore it starts, it compiles it into the most efficient code anyway.
Thus given:
for (i = 0; i < k - 1; i++)
foo ();
gcc compiled it into the equivalent of:
i = k - 1
for (int ii = i; ii > 0; i--)
foo ();
or:
i = k - 1
for (int ii = i - 1; ii >= 0; i--)
foo ();
(I forget which and can't be bothered to go back and check.)
--
Clive D.W. Feather | Home: <clive@xxxxxxxxxx>
Tel: +44 20 8495 6138 (work) | Web: <http://www.davros.org>
Fax: +44 870 051 9937 | Work: <clive@xxxxxxxxx>
Please reply to the Reply-To address, which is: <clive@xxxxxxxxxx>
.
- Follow-Ups:
- Re: strlen(), K+1: clarification
- From: Richard Harter
- Re: strlen(), K+1: clarification
- From: Richard Harter
- Re: strlen(), K+1: clarification
- References:
- strlen(), K+1: clarification
- From: spinoza1111
- Re: strlen(), K+1: clarification
- From: Ben Bacarisse
- Re: strlen(), K+1: clarification
- From: spinoza1111
- Re: strlen(), K+1: clarification
- From: Willem
- Re: strlen(), K+1: clarification
- From: Randy Howard
- Re: strlen(), K+1: clarification
- From: Willem
- Re: strlen(), K+1: clarification
- From: spinoza1111
- Re: strlen(), K+1: clarification
- From: Stephen Howe
- Re: strlen(), K+1: clarification
- From: Richard Heathfield
- Re: strlen(), K+1: clarification
- From: spinoza1111
- Re: strlen(), K+1: clarification
- From: Willem
- Re: strlen(), K+1: clarification
- From: Richard Harter
- strlen(), K+1: clarification
- Prev by Date: Re: Dealing with ad hominem attacks in comp.programming
- Next by Date: Re: Working as programmer with Bachelor's degree
- Previous by thread: Re: strlen(), K+1: clarification
- Next by thread: Re: strlen(), K+1: clarification
- Index(es):
Relevant Pages
|