Last active
September 1, 2017 19:34
-
-
Save chrisbrandow/c845e94206b992172522422a7fd6e964 to your computer and use it in GitHub Desktop.
NSString vs. NSData Text File Parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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