Skip to content

Instantly share code, notes, and snippets.

@ashfurrow
Created June 17, 2013 14:38
Show Gist options
  • Save ashfurrow/5797375 to your computer and use it in GitHub Desktop.
Save ashfurrow/5797375 to your computer and use it in GitHub Desktop.
Array access performance tests
/*
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