Skip to content

Instantly share code, notes, and snippets.

@carloe
Created October 17, 2016 20:33
Show Gist options
  • Save carloe/7d7f14eead5e2634445418704b7e369c to your computer and use it in GitHub Desktop.
Save carloe/7d7f14eead5e2634445418704b7e369c to your computer and use it in GitHub Desktop.
#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