As they say you need to test these things. But... the following simple test was instructive for me to get an idea of the relative difference in speed between the NSMutableDictionary and NSMutableArray collection classes in the case of small size high allocation rate examples.
Running the following program the times were: (with garbage collection on) (on a recent quad core machine)
NSMutableDictionary 4.624478 seconds
NSMutableArray 1.806365 seconds
int main (int argc, const char * argv[])
{
NSLog(@"Hello, World!");
LNCStopwatch* stopwatch = [[LNCStopwatch alloc] init];
[stopwatch start];
for (int i = 1; i< 1000000; i++)
{
NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
[dict setObject:@"a" forKey:@"a"];
[dict setObject:@"b" forKey:@"b"];
[dict setObject:@"c" forKey:@"c"];
[dict setObject:@"d" forKey:@"d"];
[dict setObject:@"e" forKey:@"e"];
[dict setObject:@"y" forKey:@"a"];
[dict setObject:@"x" forKey:@"d"];
}
[stopwatch stopAndLogTimeAndReset];
[stopwatch start];
for (int i = 1; i< 1000000; i++)
{
NSMutableArray* arr = [[NSMutableArray alloc]init];
[arr addObject:@"a"];
[arr addObject:@"b"];
[arr addObject:@"c"];
[arr addObject:@"d"];
[arr addObject:@"e"];
[arr replaceObjectAtIndex:[arr indexOfObject:@"a"] withObject:@"y"];
[arr replaceObjectAtIndex:[arr indexOfObject:@"d"] withObject:@"x"];
}
[stopwatch stopAndLogTimeAndReset];
return 0;
}
(The absolute times I don't think are really all that important, its just the relative times which are more important for these small size classes. For larger size classes of corse the nature of the collection class will dominate, eg NSMutableDictionary should be O(1) to find an element, etc... )