Created
June 17, 2013 14:38
-
-
Save ashfurrow/5797375 to your computer and use it in GitHub Desktop.
Array access performance tests
This file contains hidden or 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
/* | |
Results on iPad 4th gen with -O3 compiler optimizations: | |
2013-06-17 10:37:34.688 Labs Project[11717:907] for loop took 0.160626 seconds | |
2013-06-17 10:37:36.691 Labs Project[11717:907] enumerateObjectsUsingBlock took 0.159356 seconds | |
*/ | |
{ | |
NSInteger interations = 1000000; | |
NSMutableArray *array = [NSMutableArray array]; | |
for (int i=0; i<interations; i++) { | |
[array addObject:[NSNumber numberWithInt:arc4random()]]; | |
} | |
array = [array copy]; | |
__block int oddCount = 0; | |
CFAbsoluteTime time, end; | |
time = CFAbsoluteTimeGetCurrent(); | |
oddCount = 0; | |
for (NSNumber *number in array) { | |
[self doSomethingWithInteger:number]; | |
} | |
end = CFAbsoluteTimeGetCurrent(); | |
NSLog(@"for loop took %f seconds", end - time); | |
array = [NSMutableArray array]; | |
for (int i=0; i<interations; i++) { | |
[array addObject:[NSNumber numberWithInt:arc4random()]]; | |
} | |
array = [array copy]; | |
time = CFAbsoluteTimeGetCurrent(); | |
[array enumerateObjectsUsingBlock:^(NSNumber *obj, NSUInteger idx, BOOL *stop) { | |
[self doSomethingWithInteger:obj]; | |
}]; | |
end = CFAbsoluteTimeGetCurrent(); | |
NSLog(@"enumerateObjectsUsingBlock took %f seconds", end - time); | |
... | |
} | |
-(void)doSomethingWithInteger:(NSNumber *)number { | |
static NSInteger oddCount = 0; | |
if (number.integerValue & 1) { | |
oddCount++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment