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:
NSString‘sstringByAppendingString:versusNSMutableString‘sappendString:NSArray‘sarrayByAddingObject:versusNSMutableArray‘saddObject:- …
Thank you and happy coding!
This question is hard to answer :
NSArrayandNSStringaren’t actual implementations, they are class-clusters and so areNSMutableArrayandNSMutableString. 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 :
stringByAppendingStringandarrayByAddingObjectcreate new objects which contains the modifications, ie copy the current items to a new place in memory,NSMutableArrayandNSMutableStringshould have better performances because they are built to prevent copying when possible (not actually true becauseNSMutableArraymight 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.