Skip to content

Instantly share code, notes, and snippets.

@jmah
Created April 19, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmah/de52e99ec18eab3ba32f to your computer and use it in GitHub Desktop.
Save jmah/de52e99ec18eab3ba32f to your computer and use it in GitHub Desktop.
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
Copy link
Author

jmah commented Apr 19, 2015

Benchmark results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment