Re: Efficiently adding a bunch of items to an NSMutableArray

2010-09-22 Thread Ken Thomases
On Sep 22, 2010, at 05:46, Oleg Krupnov wrote:

> 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.

I'll add my two cents: you should almost always use the method that directly 
describes the operation you're trying to perform at a high level 
(-addObjectsFromArray:, in this case).  If there's a way that you could imagine 
breaking the high-level operation down into low-level operations, then that 
same technique is available to the implementors of the high-level routine.  
Plus, they may have techniques available to them that you don't -- they have 
access to the internals, they can put more resources into optimization, etc.  
By choosing the high-level operation, you give the implementation the greatest 
freedom to take advantage of advanced techniques.  If you choose the low-level 
techniques, you curtail the possibilities.

Also, the high-level approach will be less code, less error-prone, easier to 
read and understand, etc.

It's almost always the right choice.

And, as others have said, you're probably optimizing prematurely.  So, all of 
the other considerations probably swamp any benefit from caring about the 
performance of this single operation.

Regards,
Ken

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Efficiently adding a bunch of items to an NSMutableArray

2010-09-22 Thread Quincey Morris
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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Efficiently adding a bunch of items to an NSMutableArray

2010-09-22 Thread Uli Kusterer
On Sep 22, 2010, at 2:46 PM, Oleg Krupnov wrote:
> 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 think" and "seems" are bad advice. What do Shark/Instruments say? Does it 
take long? Does this code run so frequently that it's even worth spending time 
on optimizing it?

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Efficiently adding a bunch of items to an NSMutableArray

2010-09-22 Thread Jean-Daniel Dupas

Le 22 sept. 2010 à 14:46, Oleg Krupnov a écrit :

> Hi,
> 
> 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?


Is this part of your code a bottleneck ? If not, use the simplest way 
(-addObjectsFromArray:) and don't bother with premature optimization.

-- Jean-Daniel




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Efficiently adding a bunch of items to an NSMutableArray

2010-09-22 Thread Oleg Krupnov
Hi,

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?

Thanks,

Oleg.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com