On 24/07/2008, at 4:12 PM, Jens Alfke wrote:


On 23 Jul '08, at 9:15 PM, Steve Cronin wrote:

Is this code worth it?
if ([M count]>0) [M removeObject:O];

OR should I just do
[M remove O];

No, the first line is not worth it. You've added an extra message- send (which is _not_ cheap) to pre-check something that the NSArray (really CFArray) implementation is going to implicitly check anyway. You've also made your code harder to read, and slightly larger.


[snip]

An even better optimization, if you don't care about the order of the objects, is to use a set instead of an array. Removing an object from a large set is much, much faster than removing from a comparable array. (For NSSet it's O(1) while for NSArray it's O(n).)




I'd been wondering if there was a performance difference in general between sets and arrays - I have been building an app that uses Core Data, and ToMany relations are sets, but in some cases I have been using [xxx allObjects] (or similar, I'm not looking at the code right now).

Am I better off in general (ie, adding objects, removing objects, etc) using an NS(Mutable)Set instead of an NSArray, if I don't need sorting and duplicate items?

Matt.
--
Matthew Schinckel <[EMAIL PROTECTED]>

The Feynman Problem-Solving Algorithm:
 (1) write down the problem;
 (2) think very hard;
 (3) write down the answer.

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to