• 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
Bad performance of -commonPrefixWithString:options:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bad performance of -commonPrefixWithString:options:


  • Subject: Bad performance of -commonPrefixWithString:options:
  • From: Norbert Heger <email@hidden>
  • Date: Tue, 15 Jan 2002 03:04:02 +0100

I just discovered that the following NSString category method:

- (NSString *)commonPrefixWithString:(NSString *)aString
{
unsigned int i, count = MIN([self length], [aString length]);
for (i = 0; i < count; i++) {
unichar c1 = [self characterAtIndex:i];
unichar c2 = [aString characterAtIndex:i];
if (c1 != c2) { break; }
}
return [self substringToIndex:i];
}

performs about 30 times (!!!) faster than the equivalent NSString
implementation -commonPrefixWithString:options:


NSString *s, [s length] == 70

5000 x [s commonPrefixWithString:s] -> 0.150 sec.
5000 x [s commonPrefixWithString:s options:NSLiteralSearch] -> 4.905 sec.


If the above category method caches the function pointers returned by
-methodForSelector:@selector(characterAtIndex:) instead of invoking the
method directly, it will be even 50 times faster!


Best Regards, Norbert

_____________________________________________
Norbert Heger, Objective Development
http://www.obdev.at/


  • Follow-Ups:
    • Re: Bad performance of -commonPrefixWithString:options:
      • From: Ali Ozer <email@hidden>
  • Prev by Date: Re: NSWorkspaceDidTerminateApplicationNotification bug?
  • Next by Date: Re: Database
  • Previous by thread: Re: NSFileManager buggy?!?
  • Next by thread: Re: Bad performance of -commonPrefixWithString:options:
  • Index(es):
    • Date
    • Thread