My solution with a "search text" progression


#1

Hello to everyone,

I want to expose my solution which run a bit slowly on my macbook pro.

[code] NSString *chaineListePrenoms = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL];
NSArray *listePrenoms = [chaineListePrenoms componentsSeparatedByString:@"\n"];
NSString *chaineListeMotsDictionnaire = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL];
// because for the first word we search a \n…\n and we want to be sure that the last line ends with a \n
if ( ![[chaineListeMotsDictionnaire substringWithRange:NSMakeRange([chaineListeMotsDictionnaire length]-2, 2)] isEqual: @"\n"])
chaineListeMotsDictionnaire = [NSString stringWithFormat:@"\n%@", chaineListeMotsDictionnaire];
else
chaineListeMotsDictionnaire = [NSString stringWithFormat:@"\n%@\n", chaineListeMotsDictionnaire];

    NSDate *heureDebut =  [NSDate date];
    
    int nombrePrenomsDansDictionnaire=0;
    for (NSString *prenomAChercher in listePrenoms) {
        if ([prenomAChercher length ]) {
        // we want only the full word corresponding... so with a \n before and after in search text we are sure of that
        NSString *chaineCherchee=[NSString stringWithFormat:@"\n%@\n", [prenomAChercher lowercaseString]];
        NSRange recherche = [chaineListeMotsDictionnaire rangeOfString:chaineCherchee options:NSLiteralSearch];
        if (recherche.location != NSNotFound) {
            nombrePrenomsDansDictionnaire++;
            // because we have alphabetical ordered lines in words and prenom files we could cut the starting string
            chaineListeMotsDictionnaire = [chaineListeMotsDictionnaire substringFromIndex:recherche.location + [chaineCherchee length]];
        }
        }
    }
    
    NSTimeInterval tempsEcoule = [heureDebut timeIntervalSinceNow];
    NSLog(@"We found %d first names (%.2f %%) which are also common words in %.2f sec", nombrePrenomsDansDictionnaire, nombrePrenomsDansDictionnaire*100.0/[listePrenoms count], tempsEcoule);

[/code]

it runs in about 16 sec without optimisation (the text cut) and about 8 with this little optimisation.
Is there any way to improve it more with this kind of text search only method ?

Regards
Stéphane