Re: getting bytes out of NSData
Re: getting bytes out of NSData
- Subject: Re: getting bytes out of NSData
- From: Sherm Pendley <email@hidden>
- Date: Fri, 22 Jul 2005 20:47:03 -0400
On Jul 22, 2005, at 8:26 PM, Koen van der Drift wrote:
Which of these two methods is faster/more efficient to get bytes
out of an NSData object:
const char *data;
data = (const char *)[myData bytes];
aSymbol = [self symbolForChar: data[i]];
or:
unsigned char buffer;
[myData getBytes: &buffer range: NSMakeRange( i, 1 )];
aSymbol = [self symbolForChar: buffer];
Well, -bytes returns a pointer to the NSData's buffer, while -
getBytes: makes a *copy* of the buffer. The overhead of making the
copy is trivial for a one-byte buffer, but there's also the
additional function call to NSMakeRange() to consider. And there's
probably a call to strncpy() or the like in there somewhere too.
That's a lot of stack thrashing just to get one byte.
Frankly though, I doubt it's going to make a significant difference
either way, unless you're doing this a few million times. If you
*are* doing this a lot, you might consider making a single call to -
bytes, and using simple pointer arithmetic after that, if you can -
much faster than repeatedly calling -bytes.
Of course, if you haven't done so already, you absolutely should
profile your app with Shark before you optimize *anything*.
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden