• 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: Efficiently getting characters
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Efficiently getting characters


  • Subject: Re: Efficiently getting characters
  • From: Bill Bumgarner <email@hidden>
  • Date: Tue, 2 May 2006 15:58:24 -0700

On May 2, 2006, at 3:40 PM, John Nairn wrote:
I am using NSString method

     - (void)getCharacters:(unichar *)buffer range:(NSRange)aRange

to get characters from a long string. Unfortunately, there is a note in the documentation that says

The abstract implementation of this method uses characterAtIndex: repeatedly,
correctly extracting the characters, though very inefficiently. Subclasses should
override it to provide a fast implementation.


but there is no indication how to do it efficiently (or why NSString didn't bother to do it efficiently in the first place)

There isn't any indication that concrete subclasses within the NSString class cluster don't provide an efficient implementation through the overriding method, as indicated in the documentation.


The whole point of a class cluster is to provide a very minimal set of core methods against which the rest of the API is implemented through generic, and often inefficient algorithms. When subclassing, your subclass can choose to override any method in the API that you determine you need a more optimal implementation.

This is exactly what happens with NSString.  When you say...

	[NSString stringWithFormat: @"Foo %@", @"bar"]

.... you do not get an instance of NSString back. Instead, you will get an instance of some specific subclass that is optimized around the storage and access needs implied by the instantiation. That subclass may very likely provide an optimized implementation of the above method.

Or not -- given that it is only a 7 character string, it may just go the inefficient route and other subclasses might be optimized to the large string case.

What is the best way to extract a range of characters from an NSString? The problem is important when I open a large file which could involve extracting thousands or millions of ranges of characters.

In that case, I don't think you will find that your performance is going to be impacted by the use of NSString. Actually, NSString is probably the worst choice because it is going to end up dragging in all those millions of characters from the file into memory.


You probably want to use NSData's dataWithContentsOfMappedFile:, assuming your files are small enough to be mapped into your app without exhausting your app's VM.

Or, if the file's are just totally bloody huge, then you could use NSFileHandle if you are largely going to be seeking and reading bits of data to immediately massage them into a different form (thereby necessitating a copy).

Or you might want to dive down to the VM APIs and use mmap() and friends directly.

b.bum
_______________________________________________
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


References: 
 >Efficiently getting characters (From: John Nairn <email@hidden>)

  • Prev by Date: Re: Efficiently getting characters
  • Next by Date: Re: Efficiently getting characters
  • Previous by thread: Re: Efficiently getting characters
  • Next by thread: Re: Efficiently getting characters
  • Index(es):
    • Date
    • Thread