Created
June 11, 2014 14:09
-
-
Save nickcharlton/7020be064021365464de to your computer and use it in GitHub Desktop.
strptime_l vs. NSDateFormatter Benchmarks
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
#import <Foundation/Foundation.h> | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
static size_t const iterations = 10000; | |
uint64_t t_0 = dispatch_benchmark(iterations, ^{ | |
@autoreleasepool { | |
NSString *dateStr = @"Sat Sep 15 05:52:10 +0000 2012"; | |
const char *str = [dateStr UTF8String]; | |
const char *fmt = [@"%a %b %d %H:%M:%S %z %Y" UTF8String]; | |
struct tm timeinfo; | |
memset(&timeinfo, 0, sizeof(timeinfo)); | |
char *ret = strptime_l(str, fmt, &timeinfo, NULL); | |
NSDate *date = nil; | |
if (ret) { | |
time_t time = mktime(&timeinfo); | |
date = [NSDate dateWithTimeIntervalSince1970:time]; | |
} | |
} | |
}); | |
NSLog(@"strptime_l Avg. Runtime: %llu ns", t_0); | |
uint64_t t_1 = dispatch_benchmark(iterations, ^{ | |
@autoreleasepool { | |
NSString *dateStr = @"Sat Sep 15 05:52:10 +0000 2012"; | |
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; | |
[dateFormat setDateFormat:@"EE LLLL d HH:mm:ss Z yyyy"]; | |
NSDate *date = [dateFormat dateFromString:dateStr]; | |
} | |
}); | |
NSLog(@"NSDateFormatter Avg. Runtime: %llu ns", t_1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gives the following result on a reasonably new MacBook Pro: