views:

723

answers:

3

I'm working on an application, I have a NSMutableArray that stores 20 or so sprites. I need to iterate through it fairly quickly and efficiently. The speed for my purposes is not optimal... I'm getting two values from each sprite as it iterates through, would it be more efficient (faster) to iterate through an array of say CGPoints then an array of sprites? Or instead of CGPoint make a custom class for the sole purpose of handling just two integer values. Either way, is speed affected by the type of objects or values stored in an array?

+8  A: 

I don't think you should be worried about iteration speed over only 20 items.

Eimantas
Agreed. Don't sweat about this unless n could be arbitrarily large, if you're iterating a LOT, or if it will someday be much larger than 20. If it's going to be ~20 for sure, look for other places to direct your optimization efforts.
Ellie P.
See I wouldn't normally but I'm working on a game, and this method that iterates through the array is getting called roughly 2 times a second. So speed variations are getting picked up by the response time of the game, it's not very noticeable but still some may be bothered by it. I made a C array of CGPoints and noticed an improvement... Thanks though for your assistance though.
BTN
+5  A: 

The speed of iteration is not affected by what type of data you're storing in the array. It is affected by the type of array you're using: there are significant differences between iterating through an Objective-C NSArray (or any of its subclasses, such as NSMutableArray), a C-style array, or a C++ std::vector.

If you're using an NSArray, and you're using Objective-C 2.0 (e.g. on the iPhone or on Mac OS X 10.5 or later), you can use fast enumeration to iterate, which is significantly faster than the older style of iteration:

// Fast enumeration
for(id object in myNSArray)
    ;  // do stuff with object

// Slow enumeration
int count = [myNSArray count], i;
for(i = 0; i < count; i++)
{
    id object = [myNSArray objectAtIndex:i];
    // do stuff with object
}

I'm not sure how fast enumeration compares with C-style arrays or C++ std::vectors, but I would bet that it's still a little bit slower. It's slower because you have the extra overhead of Objective-C: passing messages (such as count and objectAtIndex:) goes through the Objective-C runtime, which is slower than bare pointer arithmetic even in the best case.

Iterating through a C-style array or a C++ std::vector is very fast, since the compiler can optimize them to really simple pointer arithmetic instructions with no overhead:

// C-style array
SomeType *myArray = ...;  // e.g. malloc(myArraySize * sizeof(SomeType))
int i;
for(i = 0; i < myArraySize; i++)
    ; // do stuff with myArray[i]

// C++ vector
std::vector<SomeType> myArray = ...;
for(std::vector<SomeType>::iterator i = myArray.begin(); i != myArray.end(); ++i)
    ; // do stuff with *i
Adam Rosenfield
Then perhaps my answer would be a C array of CGPoints?
BTN
Thank You for your help.
BTN
Is fast enumeration really faster than the old way of doing loops in obj-c? Is it not just syntactic sugar for the same thing?
dbachrach
It is faster. In fact it is syntactic sugar for getting an bare array out of the iterator and iterate over that. As it is explained in Adams link. On the other Hand that means that this nice syntax can't be used to iterate other some collection of unknown length. So it is something completely different than a python for ... in ...: or a Java for(... : ...).
nils
A: 

this post should have gone somewhere else, so i deleted it.

nils