Last active
December 12, 2015 08:08
-
-
Save rnystrom/4741259 to your computer and use it in GitHub Desktop.
Testing using JSON data vs an NSObject with properties. Testing initialization of NSObjects and calculations of keyPath values using @AvG. Tested with CodeRunner: http://krillapps.com/coderunner/
Results: http://bit.ly/12zlUNp
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 Hospital : NSObject | |
@property (nonatomic, strong) NSString *name; | |
@property (nonatomic, strong) NSString *address; | |
@property (nonatomic, strong) NSString *city; | |
@property (nonatomic, strong) NSString *county; | |
@property (nonatomic, strong) NSString *state; | |
@property (nonatomic, strong) NSNumber *annualVisitors; | |
@property (nonatomic, strong) NSString *zip; | |
- (id)initWithJSON:(NSDictionary *)json; | |
@end | |
@implementation Hospital | |
- (id)initWithJSON:(NSDictionary *)json { | |
if (self = [super init]) { | |
self.name = json[@"Hospital"]; | |
self.address = json[@"Address"]; | |
self.city = json[@"City"]; | |
self.county = json[@"County"]; | |
self.state = json[@"State"]; | |
self.annualVisitors = json[@"AnnualERVisitors"]; | |
self.zip = json[@"Zip"]; | |
} | |
return self; | |
} | |
@end | |
CGFloat runWithObject() { | |
NSArray *hospitalJSON = @[@{@"Hospital" : @"JACKSON MEDICAL CENTER", | |
@"Address" : @"220 Hospital Drive", | |
@"City" : @"Jackson", | |
@"County" : @"Clarke", | |
@"State" : @"AL", | |
@"AnnualERVisitors" : @(3322), | |
@"Zip" : @"36545"}, | |
@{@"Hospital" : @"JACKSON MEDICAL CENTER 2", | |
@"Address" : @"227 Hospital Drive", | |
@"City" : @"Jackson", | |
@"County" : @"Clarke", | |
@"State" : @"AL", | |
@"AnnualERVisitors" : @(6422), | |
@"Zip" : @"36545"}, | |
@{@"Hospital" : @"PASCO REGIONAL MEDICAL CENTER", | |
@"Address" : @"13100 Ft King Road", | |
@"City" : @"Dade City", | |
@"County" : @"PASCO", | |
@"State" : @"FL", | |
@"AnnualERVisitors" : @(4125), | |
@"Zip" : @"33525"}, | |
@{@"Hospital" : @"MACNEAL HOSPITAL", | |
@"Address" : @"3249 South Oak Park Avenue", | |
@"City" : @"Berwyn", | |
@"County" : @"COOK", | |
@"State" : @"IL", | |
@"AnnualERVisitors" : @(4432), | |
@"Zip" : @"60402"}]; | |
NSMutableArray *arr = [NSMutableArray array]; | |
for(id raw in hospitalJSON) { | |
Hospital *hospital = [[Hospital alloc] initWithJSON:raw]; | |
[arr addObject:hospital]; | |
} | |
NSDate *start = [NSDate date]; | |
NSNumber *avg = [arr valueForKeyPath:@"@avg.annualVisitors"]; | |
NSDate *finish = [NSDate date]; | |
return [finish timeIntervalSinceDate:start]; | |
} | |
CGFloat runWithDictionary() { | |
NSArray *hospitalJSON = @[@{@"Hospital" : @"JACKSON MEDICAL CENTER", | |
@"Address" : @"220 Hospital Drive", | |
@"City" : @"Jackson", | |
@"County" : @"Clarke", | |
@"State" : @"AL", | |
@"AnnualERVisitors" : @(3322), | |
@"Zip" : @"36545"}, | |
@{@"Hospital" : @"JACKSON MEDICAL CENTER 2", | |
@"Address" : @"227 Hospital Drive", | |
@"City" : @"Jackson", | |
@"County" : @"Clarke", | |
@"State" : @"AL", | |
@"AnnualERVisitors" : @(6422), | |
@"Zip" : @"36545"}, | |
@{@"Hospital" : @"PASCO REGIONAL MEDICAL CENTER", | |
@"Address" : @"13100 Ft King Road", | |
@"City" : @"Dade City", | |
@"County" : @"PASCO", | |
@"State" : @"FL", | |
@"AnnualERVisitors" : @(4125), | |
@"Zip" : @"33525"}, | |
@{@"Hospital" : @"MACNEAL HOSPITAL", | |
@"Address" : @"3249 South Oak Park Avenue", | |
@"City" : @"Berwyn", | |
@"County" : @"COOK", | |
@"State" : @"IL", | |
@"AnnualERVisitors" : @(4432), | |
@"Zip" : @"60402"}]; | |
NSDate *start = [NSDate date]; | |
NSNumber *avg = [hospitalJSON valueForKeyPath:@"@avg.AnnualERVisitors"]; | |
NSDate *finish = [NSDate date]; | |
return [finish timeIntervalSinceDate:start]; | |
} | |
NSInteger const runCount = 1000000; | |
void testObject() { | |
NSDate *objectStart = [NSDate date]; | |
CGFloat executionRunTime = 0.f; | |
for (int i = 0; i < runCount; i++) { | |
executionRunTime += runWithObject(); | |
} | |
NSLog(@"Execution time: %f",executionRunTime); | |
NSDate *objectFinish = [NSDate date]; | |
NSTimeInterval objectExecutionTime = [objectFinish timeIntervalSinceDate:objectStart]; | |
NSLog(@"Total Object time: %f", objectExecutionTime); | |
} | |
void testDictionary() { | |
NSDate *dictionaryStart = [NSDate date]; | |
CGFloat executionRunTime = 0.f; | |
for (int i = 0; i < runCount; i++) { | |
executionRunTime += runWithDictionary(); | |
} | |
NSLog(@"Execution time: %f",executionRunTime); | |
NSDate *dictionaryFinish = [NSDate date]; | |
NSTimeInterval dictionaryExecutionTime = [dictionaryFinish timeIntervalSinceDate:dictionaryStart]; | |
NSLog(@"Total Dictionary time: %f", dictionaryExecutionTime); | |
} | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
// only run 1 test at a time | |
testObject(); | |
// testDictionary(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment