views:

225

answers:

2

I've been programming C/C++ for 20 years but I'm new to objective-c and Coco and have some questions about the performance of NSMutableArray and NSArray.

I need an container much like the stl list container, but coco doesn't seem to have one and inserting and removing elements from the middle of MSMutableArray seems slow, but treating it like a stl vector isn't that fast either.

Also, MSMutableArray seems to only store Objects, so keep tracking of a simple data type line int or float incurs additional overhead of creating Objects to store them in.

I'm currently writing a game and performance is critical. I can always drop into C++ for the guts of my game engine, but there is so much about objective-c that I like and I'd hate to abandon it.

Are there fast stl like containers for objective-c that I should be using?

Is there any good online documentation about how these are implemented so I can try and figure put why performance is so slow?

Thanks!

+1  A: 

The foundation collection classes like NSMutableArray aren't guaranteed to use any specific implementation, and in fact I believe they will actually change their data structure to optimize speed for the number of items they contain. You can read more here, but basically I wouldn't worry about the underlying algorithm.

You could create your own data structure in C (or go the Objective-C++ route if you want), which would save the memory and performance overhead of wrapping each primitive in an NSNumber. Of course, you'll want to run the performance tools first to make sure this is really where the problem lies, and you're not just wasting your time.

Marc Charbonneau
A: 

If you really cares about performance, I recommend to avoid using Foundation data structures via Objective-C as the runtime system eliminates all opportunities for compile-time optimization, and as you've said you always need to box primitives.

You can just use Objective-C++ and use std::list<id>.

If you must use Objective-C, see CHDataStructures, which contains the CHSinglyLinkedList type.

KennyTM