Objective-C Array Iteration Speed

I am working on an application, I have an NSMutableArray that stores 20 or so sprites. I need to do this fairly quickly and efficiently. The speed for my purposes is not optimal ... I get two values ​​from each sprite as it iterates, would it be more efficient (faster) to iterate through an array of CGPoints and then an array of sprites? Or instead of CGPoint, create a custom class for the sole purpose of handling only two integer values. In any case, the speed depends on the type of objects or values ​​stored in the array?

+3
source share
2 answers

I don’t think you should worry about the speed of iteration with only 20 points.

+9
source

The speed of iteration does not depend on what type of data you store in the array. It is affected by the type of array used: there are significant differences between iterating through Objective-C NSArray(or any of its subclasses, for example NSMutableArray), a C-style array, or C ++ std::vector.

If you use NSArrayand use Objective-C 2.0 (for example, on iPhone or Mac OS X 10.5 or later), you can use fast enumeration to repeat, which is significantly faster than the old iteration style:

// 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
}

, C- ++ std::vector s, , . , Objective-C: ( count objectAtIndex:) Objective-C, , .

C- ++ std::vector , :

// 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
+9

Source: https://habr.com/ru/post/1724786/


All Articles