Skip to content

Instantly share code, notes, and snippets.

@chrisbrandow
Last active September 1, 2017 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbrandow/c845e94206b992172522422a7fd6e964 to your computer and use it in GitHub Desktop.
Save chrisbrandow/c845e94206b992172522422a7fd6e964 to your computer and use it in GitHub Desktop.
NSString vs. NSData Text File Parsing
- (NSArray *)stringsFromFile:(NSString *)name
{
NSDate *start = [NSDate date];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"yaml"];
NSString *fileString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSTimeInterval fileTime = [start timeIntervalSinceNow];
NSArray *lines = [fileString componentsSeparatedByString:@"\n"];
NSTimeInterval end = [start timeIntervalSinceNow];
NSLog(@"end count:%.9f %.9f, %zd", fileTime, end, offsets.count);
return lines;
}
- (void)bytesFromFile:(NSString *)name
{
NSDate *start = [NSDate date];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"yaml"];
offsets = [NSMutableArray new];
words = [[NSData alloc] initWithContentsOfFile:path options:NSDataReadingMappedAlways error:nil];
NSTimeInterval fileTime = [start timeIntervalSinceNow];
const char *bytes = [words bytes];
const char *cur = bytes;
const char *end = bytes + [words length];
[offsets addObject:@(0)];
while (cur < end) {
if (*cur++ == '\n') {
[offsets addObject:@((int)(cur - bytes))];
}
}
[offsets addObject:@((int)(end - bytes))];
NSTimeInterval stop = [start timeIntervalSinceNow];
NSLog(@"end count:%.9f %.9f, %zd", fileTime, stop, offsets.count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment