• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: getting bytes out of NSData
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: getting bytes out of NSData
      • From: Koen van der Drift <email@hidden>
    • Re: getting bytes out of NSData
      • From: Finlay Dobbie <email@hidden>
References: 
 >getting bytes out of NSData (From: Koen van der Drift <email@hidden>)

  • Prev by Date: Getting Sleep Events
  • Next by Date: Re: NSImage question
  • Previous by thread: getting bytes out of NSData
  • Next by thread: Re: getting bytes out of NSData
  • Index(es):
    • Date
    • Thread