Skip to content

Instantly share code, notes, and snippets.

@jmah
Created April 19, 2015 23:19
Show Gist options
  • Select an option

  • Save jmah/de52e99ec18eab3ba32f to your computer and use it in GitHub Desktop.

Select an option

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

jmah commented Apr 19, 2015

Copy link
Copy Markdown
Author

Benchmark results

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