I implement an algorithm for populating filled queues and must store and retrieve pairs of numbers in NSMutableArray .
Basically, I create an array
m_queue = [NSMutableArray array];
then at some point i fill the array
[m_queue addObject:[NSValue valueWithCGPoint:CGPointMake(x + 1, y)]]
then I get the data for the next iteration and delete the value at the beginning of the array
NSValue* value = [m_queue objectAtIndex:0]; [m_queue removeObjectAtIndex:0]; CGPoint nextPoint = [value CGPointValue]; [self queueFloodFill8:nextPoint.xy:nextPoint.y];
The question is: what can I do to avoid creating a large number of CGPoint and NSValue ?
I do not need points, the algorithm uses pairs of integer values, so I think that there may be a better way to store such pairs.
UPDATE: I studied the implementation of a C-style solution like @mattjgalloway and @CRD.
I imagined
typedef struct lookup_point_struct { int x; int y; struct lookup_point_struct* next; } LookupPoint;
and rewritten code to use a linked list of such structures instead of NSMutableArray and CGPoint / NSValue .
All this made my code about 3 times faster. And memory consumption has also dropped significantly.
source share