On Wed, 7 Feb 2007, Steve wrote:
> Which got me thinking that maybe a standard while loop is not the
> proper solution here.  So I turned it on it's head with a do while
> which gave me 100,000 which is a pretty good speed up!
> 
>       do{
>               s[y] = ~s[y];
>       }while(y++ <= s.length());

Unless I misunderstand the meaning of the value returned by s.length(), 
that do/while loop twiddles two characters past the end of the data in the 
array.

My presumption is that if s.length() returns 3, for example, the valid 
array indices would be 0, 1, and 2.  If s.length() returns 3, however, the 
loop above would twiddle the array entries at indices 0, 1, 2, 3, and 4.  
Putting the test at the end of the loop results in a double-whammy from 
the use of both post-increment and '<='.

If you're fond of do/while, try this instead:
        do {} while(++y < s.length());

Chris

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to