Skip to content

Instantly share code, notes, and snippets.

@nickcharlton
Created June 11, 2014 14:09
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 nickcharlton/7020be064021365464de to your computer and use it in GitHub Desktop.
Save nickcharlton/7020be064021365464de to your computer and use it in GitHub Desktop.
strptime_l vs. NSDateFormatter Benchmarks
#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);
}
}
@nickcharlton
Copy link
Author

This gives the following result on a reasonably new MacBook Pro:

strptime_l Avg. Runtime: 21919 ns
NSDateFormatter Avg. Runtime: 405682 ns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment