In my app I have to store path points into an array and then follow those points. To get a smoother run I usually have to dump a path into 5k points. This means I have to store 10k floats- 5k for x and 5k for y coordinates. Right now this is what I’m doing:
1.In the view load I initialize an NSArray with those 10k numbers like this:
pathPoints=[NSArray arrayWithObjects:[NSNumber numberWithFloat:-134.8427], [NSNumber numberWithFloat:148.8433], ....... and so on];
-
And then I read it like this:
int currentXIndex=..////
[[pathPoints objectAtIndex:currentXIndex] floatValue];
[[pathPoints objectAtIndex:currentXIndex+1] floatValue];
As you can see, each time when I need the next position? I have to unbox it(cast it from NSNumber to float). And I’m sure this takes a great deal of performance. Any suggestions how I can do this another, more performant way?
For a simple container, I’d use a C array and traverse with pointers. Remember that Objective C is a superset of C, so you have everything right there for when the need arise.
edit; sample code:
you don’t have to store all numbers ‘naked’, a
structdon’t have any overhead:then just
malloc()the size needed:and iterate like any simple array:
If you don’t like the “old C” look of the code, it’s easy to encapsulate all this in a class that ‘owns’ the array (don’t forget to
free()it when disposed)