• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is checking -count worth it?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is checking -count worth it?


  • Subject: Re: Is checking -count worth it?
  • From: Matthew Schinckel <email@hidden>
  • Date: Thu, 24 Jul 2008 16:32:34 +0930


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@hidden>

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 (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Is checking -count worth it?
      • From: Jens Alfke <email@hidden>
    • Re: Is checking -count worth it?
      • From: Ken Thomases <email@hidden>
References: 
 >Is checking -count worth it? (From: Steve Cronin <email@hidden>)
 >Re: Is checking -count worth it? (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Help on Menu Item
  • Next by Date: Re: Sent Actions, Delegates, Outlets
  • Previous by thread: Re: Is checking -count worth it?
  • Next by thread: Re: Is checking -count worth it?
  • Index(es):
    • Date
    • Thread