Skip to content

Instantly share code, notes, and snippets.

@jmah jmah/DictEnumTests.m
Created Apr 19, 2015

Embed
What would you like to do?
NSDictionary Enumeration Benchmark
//
// 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
@jmah

This comment has been minimized.

Copy link
Owner Author

commented Apr 19, 2015

Benchmark results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.