Skip to content

Instantly share code, notes, and snippets.

@advantis
Last active October 10, 2015 02:37
Show Gist options
  • Save advantis/3619129 to your computer and use it in GitHub Desktop.
Save advantis/3619129 to your computer and use it in GitHub Desktop.
Enumeration benchmark
/* All measurements were done on iPhone 4 */
// MRC:
// for loop: 1.891520ms
// NSFastEnumeration: 0.393583ms
// enumerateObjectsUsingBlock: 0.547250ms
// ARC (w/o __unsafe_unretained):
// for loop: 6.889979ms
// NSFastEnumeration: 0.394479ms
// enumerateObjectsUsingBlock: 6.513708ms
// ARC (w/ __unsafe_unretained):
// for loop: 6.899520ms
// NSFastEnumeration: 0.395750ms
// enumerateObjectsUsingBlock: 0.548833ms
- (void) performTests
{
const NSUInteger ObjectCount = 10000;
const NSUInteger TestCount = 1000;
NSMutableArray *data = [NSMutableArray arrayWithCapacity:ObjectCount];
for (NSUInteger i = 0; i < ObjectCount; ++i)
{
[data addObject:[NSNumber numberWithInteger:arc4random_uniform(ObjectCount)]];
}
NSLog(@"for loop: %fms", (1. / NSEC_PER_MSEC) * ADVGetMedianExecutionTime(TestCount, ^{
for (NSUInteger i = 0; i < ObjectCount; ++i)
{
if (!data[i]) NSLog(@"impossible!");
}
}));
NSLog(@"NSFastEnumeration: %fms", (1. / NSEC_PER_MSEC) * ADVGetMedianExecutionTime(TestCount, ^{
for (id object in data)
{
if (!object) NSLog(@"impossible!");
}
}));
NSLog(@"enumerateObjectsUsingBlock: %fms", (1. / NSEC_PER_MSEC) * ADVGetMedianExecutionTime(TestCount, ^{
[data enumerateObjectsUsingBlock:^(/*__unsafe_unretained*/ id object, NSUInteger idx, BOOL *stop) {
if (!object) NSLog(@"impossible!");
}];
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment