Skip to content

Instantly share code, notes, and snippets.

@ruleroller
Last active December 19, 2015 16:14
Show Gist options
  • Save ruleroller/f81dcd1f1fcfa5091640 to your computer and use it in GitHub Desktop.
Save ruleroller/f81dcd1f1fcfa5091640 to your computer and use it in GitHub Desktop.
get the time how the program it takes by second
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
    @autoreleasepool {
       
        //  Read in files as large strings
        NSString *nameList = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                                       encoding:NSUTF8StringEncoding
                                                          error:NULL];
        NSString *wordList = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                       encoding:NSUTF8StringEncoding
                                                          error:NULL];
       
        //  Break up lists into array of strings
        NSArray *names = [nameList componentsSeparatedByString:@"\n"];
        NSArray *words = [wordList componentsSeparatedByString:@"\n"];
       
        int i = 0;
        NSDate * startTime = [NSDate date];
       
        NSSet * wordsSet = [NSSet setWithArray:words];
        NSSet * namesSet = [NSSet setWithArray:names];
        for (NSString * name in namesSet) {
            if ([wordsSet containsObject:name.lowercaseString] && ![name isEqualToString:@""]) {
                i += 1;
            }
        }
        NSDate * endTime = [NSDate date];
        double timeTaken = [endTime timeIntervalSinceDate:startTime];
        NSLog(@"Total Number of Matches: %d.\nComparison completed in %.2f seconds.", i, timeTaken);
    }
    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment