18

Do

[[NSMutableArray alloc] init];

and

[[NSMutableArray alloc] initWithCapacity:0];

compile into the exact same thing?

If they differ, then how, and which form is "better" in terms of memory and runtime performance?

Scott
  • 365
  • 3
  • 8
  • Without knowing the implementation details I would like to be able to believe and say that the Foundation team made the performance difference between the two negligible. From a personal opinion I think it is best practice to call any provided initializers by the class over a generic one if it makes sense. Since NSMutableArray publicly declares initWithCapacity and just inherits init I would lean towards using initWithCapacity. Also why not write a loop that analyzes a lot of mutable array creations using init and initWithCapacity and add your observations. – Joe May 20 '11 at 20:57
  • 3
    See also [NSMutableArray initWithCapacity nuances](http://stackoverflow.com/questions/3948062/nsmutablearray-initwithcapacity-nuances). – albertamg May 20 '11 at 21:03
  • Beware premature optimization. – NSResponder May 20 '11 at 21:18
  • Interesting read about NSArray performance: ["Our Arrays, Aren't" by Ridiculous Fish](http://ridiculousfish.com/blog/archives/2005/12/23/array/). @Joe – jscs May 20 '11 at 22:52

1 Answers1

19

No, they will not behave identically. init will create an array with some unknown but most likely nonzero capacity—whatever the authors decided was a reasonable default for most situations. initWithCapacity:0 will request an array with absolutely no allocated space. What this means is up to the NSMutableArray implementation: it might not allow a capacity of 0 and behave exactly as init, or it might not immediately allocate anything and instead allocate some amount (the same as init, possibly, or maybe not) when you first need it.

Which will perform "better" completely depends on how you expect to use the array. -init says "I have no expectations for this array; I'll either be adding to and removing from it a lot, or it will likely be pretty small." -initWithCapacity: says "I expect this array to have no more than this many elements for the foreseeable future."

About the only place where I would expect initWithCapacity:0 to be reasonable is if you are creating an array that you don't expect to fill with anything for a fairly long period of time.

Note that the standard performance caveat applies here: it's probably not an issue unless you profile and determine that it is. I can't really imagine a situation (except for thousands upon thousands of NSMutableArray objects) where the difference between these two will be substantial.

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • 5
    +1 note that using `initWithCapacity:n` does not preclude you from adding more than *n* objects to the array. It's just a suggestion so `NSArray` can try to do some optimization under-the-hood. – Dave DeLong May 20 '11 at 21:18
  • 1
    Also refer to [this blog post](http://cocoawithlove.com/2008/08/nsarray-or-nsset-nsdictionary-or.html) by Matt Gallagher – Scott May 23 '11 at 16:30