// gcc -framework Foundation -O3 -g -o dictfind3 dictfind3.m

#import <Foundation/Foundation.h>


@interface Dict : NSObject
{
    NSArray *_words;
}
- (NSArray *)find:(NSString *)toFind;
@end

@implementation Dict
- (id)init
{
    if((self = [super init]))
    {
        NSString *str = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"];
        _words = [[str componentsSeparatedByString:@"\n"] copy];
    }
    return self;
}

- (void)dealloc
{
    [_words release];
    [super dealloc];
}

- (NSArray *)find:(NSString *)toFind
{
    NSMutableArray *array = [NSMutableArray array];
    for(NSString *word in _words)
    {
        if([word rangeOfString:toFind options:NSCaseInsensitiveSearch].location != NSNotFound)
            [array addObject:word];
    }
    return array;
}
@end

int main(int argc, char **argv)
{
    NSAutoreleasePool *outerPool = [[NSAutoreleasePool alloc] init];
    
    Dict *dict = [[Dict alloc] init];
    
    NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval lastPrinted = start;
    unsigned counter = 0;
    while(1)
    {
        NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc] init];
        [dict find:@"bob"];
        counter++;
        
        NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
        if(now - lastPrinted >= 1.0)
        {
            NSLog(@"%.1f/sec", counter / (now - start));
            lastPrinted = now;
        }
        [innerPool release];
    }
    
    [dict release];
    [outerPool release];
    return 0;
}
