Skip to content

Instantly share code, notes, and snippets.

@pjrobertson
Created February 20, 2013 20:41
Show Gist options
  • Save pjrobertson/4999420 to your computer and use it in GitHub Desktop.
Save pjrobertson/4999420 to your computer and use it in GitHub Desktop.
Test on the LLVM compiler optimisation
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSURL *url = [NSURL fileURLWithPath:@"file://localhost/usr/bin/"];
NSMutableArray *a = [NSMutableArray array];
NSMutableArray *b = [NSMutableArray array];
NSDate *date = [NSDate date];
for (int i = 0; i < 99999 ; i++) {
[a addObject:[url path]];
[b addObject:[url path]];
}
NSLog(@"no resolve: %lfs", -1*([date timeIntervalSinceNow]));
// insert code here...
NSURL *url2 = [NSURL fileURLWithPath:@"file://localhost/usr/bin/"];
NSMutableArray *c = [NSMutableArray array];
NSMutableArray *d = [NSMutableArray array];
NSDate *d2 = [NSDate date];
for (int i = 0; i < 99999 ; i++) {
NSString *p = [url2 path];
[c addObject:p];
[d addObject:p];
}
NSLog(@"resolve: %lfs", -1*([d2 timeIntervalSinceNow]));
}
return 0;
}
@pjrobertson
Copy link
Author

Results: It takes twice as long for loop 1 to run than loop 2. You'd have thought LLVM would figure out that [url path] is called twice. Oh well

Notes: the only difference between the two loops is line 25 which resolves the URL path first

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