Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Mutable arrays




On Oct 19, 2008, at 10:11 PM, Graham Cox wrote:

Since mutable arrays and dictionaries expand as required when new objects are added, when should one use -initWithCapacity: methods?

I'd guess they're only worth using if you know you are about to populate one with a lot of items and the performance hit of letting them grow "naturally" is worth avoiding. There may also be private classes within the cluster that are optimised for certain numbers of objects, so this will act as a hint as to which one should be used.


My suggestion would be to never use the "-initWithCapacity:" methods unless you know that you have a good reason for using them. I would expect that in at least 99% of the times that you use collections there is no measurable difference in performance between using "- initWithCapacity:" over regular "-init".

Some general suggestions for best practices wrt. optimizations:

1) Measure first
2) Implement supposed optimization
3) Measure to see the impact of the code change
4) Based on the result of #3, either scrap your changes, or document the optimized code


Skipping over step #1 and #3 is always a mistake - It's almost always the case that you'll end up surprised by what you find! We also often forget to do #4, something that typically leads to maintenance problems later on.


j o a r


_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Mutable arrays (From: DKJ <email@hidden>)
 >Re: Mutable arrays (From: Graham Cox <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.