Re: Hex to NSString or NSData
Re: Hex to NSString or NSData
- Subject: Re: Hex to NSString or NSData
- From: Alastair Houghton <email@hidden>
- Date: Mon, 11 May 2009 00:55:36 +0100
On 10 May 2009, at 18:45, Andreas Grosam wrote:
On May 10, 2009, at 1:05 AM, Gwynne Raskind wrote:
The smallest (and likely fastest) would probably be the lookup
table version:
The smallest in terms of code, and it is probably the fasted method.
But it is also the most error prone ;)
Just for fun, I had a bash to see if I could get things to go any
faster. Assuming you don't care too much about detecting the invalid
hex case, you could do
int hexCharToNibbleAJH(int nibble)
{
static const int tbl[] = { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15,
-1, -1, -1, -1, -1, -1, -1, -1 };
return tbl[(nibble - 0x30) & 0x1f];
}
which seems slightly faster for me than the look-up table previously
suggested (I get 0.09464 versus 0.12074 for the big lookup table with -
Os).
For really extreme performance, you could probably work on multiple
characters simultaneously, either in a word or maybe using vector
instructions. I didn't bother trying that, and it's probably
pointless unless you had a few megabytes of hex to decode. Might be
fun though :-)
Of course, *all* of this is pointless if you're using -
characterAtIndex: to get the characters to decode; you're going to
spend too much time making function calls to justify the tiny speed-up
from the different versions of this function, even if you use an IMP.
You'd need to use CoreFoundation's inline string buffers, I think, for
any of this to be worthwhile in practice, and even then I bet you'd
need a pretty long string to see any significant benefit.
i.e. all of this is almost certainly horribly premature optimisation.
Personally, I'd just use an if statement.
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden