Rudolf Adamkovič
Rudolf Adamkovič

Reputation: 31486

Performance of mutable versus immutable objects

I would like to ask all the Cocoa veterans out there - is there any difference in performance between using mutable versus immutable objects in cases like:

Thank you and happy coding!

Upvotes: 3

Views: 458

Answers (1)

Olotiar
Olotiar

Reputation: 3274

This question is hard to answer : NSArray and NSString aren't actual implementations, they are class-clusters and so are NSMutableArray and NSMutableString. The true implementations underneath can't be determined and thus performances would be hard to compare. You probably won't find a definite answer to that one.

But what I would guess is : stringByAppendingString and arrayByAddingObject create new objects which contains the modifications, ie copy the current items to a new place in memory, NSMutableArray and NSMutableString should have better performances because they are built to prevent copying when possible (not actually true because NSMutableArray might recopy memory when elements are added but not every time).

I think you should trust the CoreFoundation coders on this one : you wan't to mutate objects ? Use the mutables one.

Upvotes: 1

Related Questions