• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Mutable arrays
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Mutable arrays


  • Subject: Re: Mutable arrays
  • From: j o a r <email@hidden>
  • Date: Sun, 19 Oct 2008 22:35:03 -0700


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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Mutable arrays
      • From: "Michael Ash" <email@hidden>
References: 
 >Mutable arrays (From: DKJ <email@hidden>)
 >Re: Mutable arrays (From: Graham Cox <email@hidden>)

  • Prev by Date: Detecting system preference for trackpad zooming.
  • Next by Date: Superscript attribute with CATextLayer
  • Previous by thread: Re: Mutable arrays
  • Next by thread: Re: Mutable arrays
  • Index(es):
    • Date
    • Thread