Last active
October 10, 2015 02:37
-
-
Save advantis/3619129 to your computer and use it in GitHub Desktop.
Enumeration benchmark
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
/* 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