Created
October 17, 2016 20:33
-
-
Save carloe/7d7f14eead5e2634445418704b7e369c to your computer and use it in GitHub Desktop.
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> | |
@interface Measure : NSObject | |
+ (NSTimeInterval)timing:(void (^)())blockName; | |
@end | |
@implementation Measure | |
+ (NSTimeInterval)timing:(void (^)())blockName { | |
NSDate *startDate = [NSDate date]; | |
blockName(); | |
NSDate *endDate = [NSDate date]; | |
return [endDate timeIntervalSinceDate:startDate]; | |
} | |
@end | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
NSInteger iterations = 10000; | |
NSTimeInterval stringDuration = [Measure timing:^(void) { | |
NSInteger matches = 0; | |
for(int i=0; i<iterations; i++) { | |
NSString *className = NSStringFromClass([NSDictionary class]); | |
if([className isEqualToString:@"NSDictionary"]) { | |
matches++; | |
} | |
} | |
}]; | |
NSLog(@"%f ms with 'isEqualToString:'", stringDuration); | |
NSTimeInterval classMemberDuration = [Measure timing:^(void) { | |
NSDictionary *testDict = @{}; | |
NSInteger matches = 0; | |
for(int i=0; i<iterations; i++) { | |
if([testDict isMemberOfClass:[NSDictionary class]]) { | |
matches++; | |
} | |
} | |
}]; | |
NSLog(@"%f ms with 'isMemberOfClass:'", classMemberDuration); | |
NSTimeInterval classDuration = [Measure timing:^(void) { | |
Class testClass = [NSDictionary class]; | |
NSInteger matches = 0; | |
for(int i=0; i<iterations; i++) { | |
if([NSDictionary class] == testClass) { | |
matches++; | |
} | |
} | |
}]; | |
NSLog(@"%f ms with '(ClassA == ClassB)'", classDuration); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment