Bad performance of -commonPrefixWithString:options:
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/