[OT] Money for nothing, and your bit flips for free?
Brian Hawkins
brianhks at activeclickweb.com
Thu Feb 8 09:31:53 MST 2007
For better performance you may want to treat your char* as an int* and
do the the bit flipping 4 or 8 bytes at at time depending on the
machine. On second thought you may want to use a long*. Basically
whatever gives you the max your machine can handle. What happens is
your single byte flip is changed to a 32 or 64 bit value and then
flipped by the processors 'not' operation. But by casting your string
to a 32 or 64 bit pointer you can flip 4 or 8 bytes at a time in one
operation.
If the bytes don't come out to an even multiple of 4 or 8 you will have
to do a little math and do the last bytes one at at time.
Another trick to obfuscate your data is to xor it with some key value.
Then on the other side you xor it again with the same key and out pops
your value. This is more along the lines of encryption but your key is
hard coded.
Brian
Steve wrote:
> Well that actually made it go from 110,000 to 130,000 but...
> We aren't comparing apples to apples either.
> Your code is more correct since it actually flips the entire string
> starting at position 0, whereas my code flips starting at position 1
> (by flaw).
>
> So I fixed my flaw in the only way that worked (the obvious fix of
> starting y at -1 instead of 0 causes the code to not pass through the
> function.
>
> s[y-1] = ~s[y-1];
>
> The results are precisely in between at 120,000
>
> 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());
>
> Thanks for making me take a long hard look at my loops :D
>
> Regards,
>
>
>
> On 2/7/07, Nicholas Leippe <nick at leippe.com> wrote:
>> On Wednesday 07 February 2007 16:13, Steve wrote:
>> > Turns out I had to create an assignment to get it function as I
>> wanted.
>> >
>> > s[y] = ~s[y];
>>
>> Yes, that would seem more correct.
>>
>> [snip]
>>
>> > inline void flip(std::string& s){
>> > int y = 0;
>> > while(y++ < s.length()){
>> > s[y] = ~s[y];
>> > }
>> > }
>>
>> Try this optimization:
>>
>> inline void flip(std::string& s) {
>> int len = s.length(); // compute the string length only once
>> for (int y = 0; y < len; y++) {
>> s[y] = ~s[y];
>> }
>> }
>>
>>
>> Depending on how std::string.length() is implemented, the compiler
>> may already
>> have been able to do this optimization for you. You can test by
>> comparing
>> both sets of code with optimizations turned off.
>>
>>
>> /*
>> PLUG: http://plug.org, #utah on irc.freenode.net
>> Unsubscribe: http://plug.org/mailman/options/plug
>> Don't fear the penguin.
>> */
>>
>
> /*
> PLUG: http://plug.org, #utah on irc.freenode.net
> Unsubscribe: http://plug.org/mailman/options/plug
> Don't fear the penguin.
> */
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3347 bytes
Desc: S/MIME Cryptographic Signature
Url : http://plug.org/pipermail/plug/attachments/20070208/b1dd48c5/smime.bin
More information about the PLUG
mailing list