6

Possible Duplicate:
NSMutableArray initWithCapacity nuances
Objective-c NSArray init versus initWithCapacity:0

What is the difference between following line of code? what is the exact advantage and disadvantage? suppose next I will do 3 addObject operation.

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: 5];
NSMutableArray *array = [[NSMutableArray alloc] init];
Community
  • 1
  • 1
Nom nom
  • 65
  • 2
  • 6
  • It's not an exact duplicate of either of those questions, although it is pretty close to the top one. – JeremyP Aug 07 '12 at 14:32
  • possible duplicate of [Objective-c NSArray init versus initWithCapacity:0](http://stackoverflow.com/questions/6077422/objective-c-nsarray-init-versus-initwithcapacity0), [NSMutableArray initWithCapacity: nuances](http://stackoverflow.com/questions/3948062/), – jscs Aug 07 '12 at 16:09

2 Answers2

8

Functionally both statements are identical.

In the first case, you are giving the run time a hint that you will soon be adding five objects to the array so it can, if it likes, preallocate some space for them. That means that the first five addObject: invocations may be marginally faster using the first statement.

However, there's no guarantee that the run time does anything but ignore the hint. I never use initWithCapacity: myself. If I ever get to a situation where addObject: is a significant performance bottleneck, I might try it to see if things improve.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
5

Regularly the difference with object oriented languages and arrays of varying sizes is: the overhead you will get and the page faults at the memory level.

To put this in another way imagine you have an object that requests 5 spaces in memory (just like your first example) and the second object doesn't reserve any space. Therefore when an object needs to be added to the first, there will already be space in memory for it to just fall in, on the other hand the non-allocated-object will first have to request for space on memory then add it to the array. This doesn't sound so bad at this level but when your arrays increase in size this becomes more important.

From Apple's documentation:

arrayWithCapacity:

Creates and returns an NSMutableArray object with enough allocated memory to initially hold a given number of objects. ... The initial capacity of the new array. Return Value A new NSMutableArray object with enough allocated memory to hold numItems objects.

El Developer
  • 3,345
  • 1
  • 21
  • 40
  • A good guess, but that isn't really what is happening.... see Jeremy's answer. – bbum Aug 07 '12 at 18:40
  • @bbum I think it's not the guess. It's a proper explanation. While in Jeremy's answer, he has used may be. – Saad Dec 30 '14 at 07:18
  • 1
    @Saad No, really, it is a guess. Read the source (CFFoundation is open source). If you request an array with 100,000 slots, it isn't going to necessarily allocate all those slots ahead of time. The data structure is a bit more complex than a flat array and, thus, the documentation is misleading, but not entirely wrong. – bbum Jan 02 '15 at 01:27