• 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: [NSString UTF8String] performance
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [NSString UTF8String] performance


  • Subject: Re: [NSString UTF8String] performance
  • From: Ali Ozer <email@hidden>
  • Date: Sun, 15 Feb 2004 08:49:59 -0800

I'm using the AGRegex kit to match regular expressions and with larger files it's starting to get very slow. The bottleneck seems to be in [NSString UTF8String] which it calls often. Each call to UTF8String seems to be calling both

__CFStringEncodedByteStream and
creates a new NSData

Does this conversion and copy always happen when calling UTF8String? Or do I have my string in the wrong format? I was hoping that their would be a way to make UTF8String just return a pointer to some underlying data structure.

For built-in NSString classes (those you get with [NSString alloc]), UTF8String does try to return an internal pointer if it can. However, this depends on a bunch of factors, isn't guaranteed even when it seems like it might be able to, and the fact remains that UTF8String might allocate a new buffer each time to package the result. So it is not a great choice in time-critical loops and such if it might potentially be called many thousands or millions of times, which might be the case in sorting or reg-ex type situations.

It would be best to try to rewrite the segments not to call UTF8String often; if the conversion can be done less times, on larger parts of the string? Another option for now is to call CFStringGetBytes() which allows passing in a pre-allocated buffer. (You can just pass the NSString into this call, thanks to toll-free bridging.)

Ali
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >[NSString UTF8String] performance (From: Jesse Grosjean <email@hidden>)

  • Prev by Date: [NSString UTF8String] performance
  • Next by Date: calling method via IMP that returns non id type - compiler warning
  • Previous by thread: [NSString UTF8String] performance
  • Next by thread: Re: [NSString UTF8String] performance
  • Index(es):
    • Date
    • Thread