Cocoa in the Shell

Collections and integers, a matter of performance

In this post I talk about 2 different ways to store integers values in a collection (Array, Set, Dictionary) and we will see that one of the 2 methods can give you far better performances, especially on an iPhone.

The common way to store integers into a NSMutableArray is to wrap them into NSNumber since a NSArray needs to be filled with objects.
Let’s take a simple example, a piece of code is always simpler to understand.

NSMutableArray* ar = [[NSMutableArray alloc] init];
for (NSUInteger i = 0; i < 1000; i++)
    NSNumber* n = [[NSNumber alloc] initWithUnsignedInteger:i];
    [ar addObject:n];
    [n release];
[ar release];

This is the way we usually do, and I bet everybody is annoyed to create an NSNumber each time, plus, in term of performances, it’s teh sux.

Instead we can use Core Foundation collections (CFArray, CFSet, CFDictionary) to store pointers to integers like this :

CFMutableArrayRef ar = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
for (NSUInteger i = 0; i < 1000; i++)
    CFArrayAppendValue(ar, (void*)i);

This is about the same amount of code, instead of Obj-C, it’s plain C, but who cares really ?

In term of performances it’s far better, for example to insert 1000 integers like in the examples above, the second method is about 10 times faster than the first on an iPhone 3G, so it’s not negligible on iOS.