Created
April 19, 2015 23:19
-
-
Save jmah/de52e99ec18eab3ba32f to your computer and use it in GitHub Desktop.
NSDictionary 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
// | |
// DictEnumTests.m | |
// | |
#import <Cocoa/Cocoa.h> | |
#import <XCTest/XCTest.h> | |
@interface DictEnumTests : XCTestCase | |
@end | |
@implementation DictEnumTests { | |
NSDictionary *_dict; | |
NSInteger _count; | |
} | |
- (void)setUp { | |
[super setUp]; | |
NSMutableDictionary *dict = [NSMutableDictionary new]; | |
while (dict.count < 100) { | |
dict[[NSString stringWithFormat:@"key %lu", (unsigned long)dict.count]] = @(arc4random()); | |
} | |
_dict = [dict copy]; | |
_count = 10000; | |
} | |
- (void)processKey:(NSString *)key value:(NSString *)value { | |
[key self]; [value self]; | |
} | |
- (void)testKeyEnumeration { | |
[self measureBlock:^{ | |
NSInteger count = _count; | |
while (count-- > 0) { | |
@autoreleasepool { | |
for (NSString *key in _dict) { | |
id value = _dict[key]; | |
[self processKey:key value:value]; | |
} | |
} | |
} | |
}]; | |
} | |
- (void)testAllKeysEnumeration { | |
[self measureBlock:^{ | |
NSInteger count = _count; | |
while (count-- > 0) { | |
@autoreleasepool { | |
for (NSString *key in _dict.allKeys) { | |
id value = _dict[key]; | |
[self processKey:key value:value]; | |
} | |
} | |
} | |
}]; | |
} | |
- (void)testBlockEnumeration { | |
[self measureBlock:^{ | |
NSInteger count = _count; | |
while (count-- > 0) { | |
@autoreleasepool { | |
[_dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) { | |
[self processKey:key value:value]; | |
}]; | |
} | |
} | |
}]; | |
} | |
@end |
Author
jmah
commented
Apr 19, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment