iPhone Foundation - performance implications of mutable and xxxWithCapacity:0
All of the collection classes have two versions - mutable and immutable, such as NSArray and NSMutableArray. Is the distinction merely to promote careful programming by providing a const collection or is there some performance hit when using a mutable object as opposed to immutable?
Similarly each of the collection classes has a method xxxxWithCapacity, like
[NSMutableArray arrayWithCapacity:0]
. I often use zero as the argument because it seems a better choice than guessing wrongly how many objects might b开发者_开发技巧e added. Is there some performance advantage to creating a collection with capacity for enough objects in advance? If not why isn't the function something like + (id)emptyArray
?
Is there some performance advantage to creating a collection with capacity for enough objects in advance?
If you reserve space for your mutable array ahead of time, you don't have the overhead of resizing an empty or small array as objects are added. Resizing can involve holding objects in place, setting up a sufficiently large, new chunk of space on the heap, and then possibly moving those objects to the new spot in memory.
Whereas, if you specify the space you need in advance, you don't have this resizing to deal with. You have that chunk of space and you can just put objects in there.
Is there some performance advantage to creating a collection with capacity for enough objects in advance?
When you add elements to a NSMutableArray, the array may have to resize its storage zone in order to accommodate with the insertion. Usually, arrays use specific algorithm to grow in a balanced way; grow enough to support some insertions, but not too much to avoid wasting memory.
You usually use "initWithCapacity:" when you approximately know the number of elements you will put into your array. The main advantage is that the storage allocation is done once and avoid unneeded resizing.
This storage resizing may have performance impact (as it implies an extension of the storage zone), but you should do a micro-benchmark according to your needs.
If not why isn't the function something like + (id)emptyArray?
You don't need a + (id)emptyArray
, because there is + (id)array
:
NSMutableArray *array = [NSMutableArray array];
[array addObject:[NSString string]];
[array removeAllObjects];
精彩评论