I am working on a word-based game, and I need to load my 180,000 word dictionary (1.9MB) into an array so the solving algorithm can work with it. The dictionary is simply one word per line, like this:
a
ab
abs
absolutely
etc
...
Right now I am using the following code to load that file into the array:
NSString *txtPath = [[NSBundle mainBundle] pathForResource:@"dict" ofType:@"txt"];
NSString *stringFromFile = [[NSString alloc]
initWithContentsOfFile:txtPath
encoding:NSUTF8StringEncoding
error:&error ];
for (NSString *word in [stringFromFile componentsSeparatedByString:@"\r\n"]) {
[wordsArray addObject:word];
}
This takes about 3-4 seconds on an iPhone 4. Probably even slower on older iOS devices. Is there a faster way to do this?
You can easily do this in the background, off the main thread using Grand Central Dispatch, or GCD.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),^(void){ NSString *txtPath = [[NSBundle mainBundle] pathForResource:@"dict" ofType:@"txt"]; NSString *stringFromFile = [[NSString alloc] initWithContentsOfFile:txtPath encoding:NSUTF8StringEncoding error:&error ]; for (NSString *word in [stringFromFile componentsSeparatedByString:@"\r\n"]) { [wordsArray addObject:word]; } });I wrote the enclosing dispatch code from memory, but it’s close (if not correct) and I think you get the idea.
EDIT: You can execute this code on the main thread, but what happens is a non-main-thread dispatch queue is where your code executes, thereby NOT blocking the UI.
You can improve the performance of your code here a little bit by replacing the
forloop with just this:There should be no need to iterate over the result from
-componentsSeparatedByString:(an array) just to put them into another array. With 180K words, that should be a significant time saving right there.