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?

1
  • 1
    First profile to see where the performance problem is, then optimize the hotspots. Commented Dec 5, 2009 at 2:18

2 Answers 2

9

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
9

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 

3 Comments

Then perhaps my answer would be a C array of CGPoints?
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?
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(... : ...).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.