Get Character At?

Derek Parnell derek at psych.ward
Wed Apr 25 08:52:39 PDT 2007


On Wed, 25 Apr 2007 15:52:45 +0200, Frits van Bommel wrote:

> Derek Parnell wrote:
>> On Wed, 25 Apr 2007 13:41:25 +1000, Daniel Keep wrote:
>> 
>>> Incidentally, I don't suppose you know anything about the relative
>>> performance of your method up there ^^ and the one in my article down
>>> here vv:
>> 
>> It seems that your routine is about 3 times slower than the one I had
>> shown. Here is my test program ... I modified your routine slightly because
>> the idiom "if (x++ == n)" is a dangerous one as it is unclear if 'x' gets
>> incremented before or after the comparision. I changed it to be more clear.
> 
> How is it unclear? Postfix-increment clearly means that the value before 
> incrementation is returned (and thus compared to n in that expression).

Yes, I know what it is supposed to do, but when written as it is, it can
either be mistakenly thought that the variable gets incremented before the
comparision or requires that extra bit of thinking to 'see' the process
flow. For that reason, I prefer to either have ++ written as its own
statement or write it so the casual reader can explicitly see the process
flow. 

For example, in the original code by Daniel, I was unsure as to whether he
was using a 0-based index or a 1-based index, as I had done in my example.
The code he supplied assumed a 0-based if the ++ worked as you describe but
it assumed a 1-based index if it worked the other way. As my example was
1-based, and I assumed that Daniel knew how to use ++ correctly, I figured
he had thus changed my definition of the Position parameter. But the point
is, because it was not absolutely clear what the *intention* of the Daniel
was, I decided to coded it so the intention was more clear.


> I think you can change these last two statements to just:
 ... 
> So my version is even faster (about 30%), at least on my machine. And 
> IMHO it's also more readable. No need to know what "stride" is, for example.

Well, if we were really into a pissing contest, we'd both remove the calls
to library routines and code it inline, in assembler etc ... but that was
not the point. Daniel's code is another example of 'foreach' not producing
the best machine code to solve the problem at hand.

-- 
Derek Parnell
Melbourne, Australia
"Justice for David Hicks!"
skype: derek.j.parnell


More information about the Digitalmars-d-learn mailing list