It is currently Wed Dec 13, 2017 2:50 am

Mulle kybernetiK Optimization

Forum for Optimizations around Cocoa and Mac OS X

multiple string equality testing

Optimizations concerning <i>Foundation</i> or <i>Objective-C</i> and its runtime
 
Posts: 2
Joined: Thu Oct 26, 2006 10:28 am

multiple string equality testing

Post by altimac » Thu Oct 26, 2006 10:57 am

I encounter a frequent situation and i never knows which implementation is the fastest:

if have to test in an NSString* pattern is not equal to several other NSStrings. Currently i'm balanced between these 2 implementations:

NSString* pattern = @"XXXXXXX";
if(![pattern isEqualToString:a] && ![pattern isEqualToString:b] && ![pattern isEqualToString:c] && ....)
[self doSomeWork];

and

NSSet* forbiddenStringsSet = [NSSet setWithObjects:a,b,c,.......,nil]; (can be STATICALLY SET, ONCE FOR ALL !!!! of course)
if(![forbiddenStringsSet containsObject:pattern])
[self doSomeWork];


Of course, it all depends on the number of strings to tests...
What is your opinion?
Is it possible to have a graph, something like the famous for() vs NSEnumerator ... ? i have no idea how to implement such a test :)

thanks!

User avatar
 
Posts: 42
Joined: Fri Aug 06, 2004 9:20 am
Location: Bochum
Website: http://www.mulle-kybernetik.com/weblog

Re: multiple string equality testing

Post by Nat! » Tue Oct 31, 2006 11:20 am

Well yes, it depends on the number of strings you want to match against.

To simplify your code a bit (this removes the negation, it's easier for my head)

Code: Select all

if( [pattern isEqualToString:a])
   continue;
if( [pattern isEqualToString:b])
   continue;
if( [pattern isEqualToString:c])
   continue;

[self doStuff];


Some things to consider:
If "pattern" were static and the number of comparisons is statistically (!) large (>3 i would say on a hunch), you could IMP cache isEqualToString:.

If "a", "b", "c" are static you can use hash to your advantage. Precalculate the hash values into a C-Array and compare those. Only if a hash matches, check with isEqualToString:. You could use the imp caching idea from above, but make sure that a, b, c are of the same class using NSAsserts. (This is basically like an NSSet, just likely to be faster for not too many abcs)

You could use a NSHashTable instead of NSSet.



To measure something like this is easy.

Write a loop, wrapped in NSLog statements

Code: Select all

NSLog( @"start");
   for( i = 0; i< 100000; i++)
   {
      // test code
   }
   NSLog( @"stop");

Note the time that is spent between @start and @stop, that's it.

 
Posts: 2
Joined: Thu Oct 26, 2006 10:28 am

Post by altimac » Tue Oct 31, 2006 11:39 am

thanks Nat!

I knew you would be the one to ask!


Return to “Foundation & Objective-C”

Who is online

Users browsing this forum: No registered users and 1 guest