Re: Efficiently adding a bunch of items to an NSMutableArray
Re: Efficiently adding a bunch of items to an NSMutableArray
- Subject: Re: Efficiently adding a bunch of items to an NSMutableArray
- From: Quincey Morris <email@hidden>
- Date: Wed, 22 Sep 2010 10:55:06 -0700
On Sep 22, 2010, at 05:46, Oleg Krupnov wrote:
> I have a NSMutableArray and need to add a number of elements to it,
> and their quantity I know in advance (in fact, they come from another
> array).
>
> I think that if I add them one-by-one in a loop, the array will have
> to reallocate its internal memory frequently. This seems inefficient.
>
> I'd like to avoid this and instead have the array to re-allocate its
> memory only once to increment the capacity by the known value.
>
> I could use "arrayWithCapacity" creation method, but the array already
> exists. It seems that the -addObjectsFromArray: method may be what I
> need, but the docs do not make it clear if it's internally optimized
> to do only a single memory re-allocation. Have anyone tested this?
In addition to the other responses you got ...
1. You *don't* have a NSMutableArray -- you have a concrete subclass of NSMutableArray. You can't *reliably* depend on which one, which means you can't reliably depend on the measured performance characteristics across OS versions, architectures and (for all we know) phases of the moon.
2. 'addObjectsFromArray' isn't one of the 5 primitive array methods, so it's implemented (in the abstract NSMutableArray superclass) as a loop over individual insertions using the 'insertObject:atIndex:' primitive. That method is almost certainly overridden in the common frameworks-supplied concrete subclasses to insert the objects with less overhead than multiple invocations of the primitive.
3. It's known that the frameworks-supplied insertion algorithm takes different paths according to the size of the array, and thus has varying performance characteristics. You can't *reliably* predict either where the cutoff sizes are or what the performance characteristics are going to be.
All you really know is that frameworks-supplied NSMutableArray subclasses are designed to perform pretty well across a broad range of conditions, and that using 'addObjectsFromArray' probably gives the implementation(s) their best opportunity to optimize for multiple insertions.
If you measure performance problems in your application, you can't *reliably* solve them by choosing a different set of NSMutableArray methods (e.g. 'arrayWithCapacity' vs 'addObjectsFromArray'). If you can't solve such performance problems at a higher level, and the problems are sufficiently critical, then NSMutableArray is probably not the correct solution anyway.
(In practice, though, the situation is not as dire as I've made it sound.)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden