• 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
NSPredicate performance considerations
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSPredicate performance considerations


  • Subject: NSPredicate performance considerations
  • From: Prachi Gauriar <email@hidden>
  • Date: Sun, 20 Nov 2005 18:35:18 -0500

Hi everyone. I've finally gotten around to playing with Core Data and I had a general question about performance considerations when using predicates. If I need to retrieve objects that fulfill a certain condition, say all objects in a to-many relationship whose value for property p is less than c, is it better to get all the objects and manually do the comparison or use a predicate?

For example, is this (typed in Mail):

NSSet *values = [self valueForKey:@"values"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"value < % u", maxVal];
NSArray *matchedVals = [[values allObjects] filteredArrayUsingPredicate:predicate];


generally recommended over this:

NSEnumerator *enumerator = [[self valueForKey:@"values"] objectEnumerator];
NSMutableArray *matchedVals = [[NSMutableArray alloc] init];
id object = nil;


while (object = [enumerator nextObject]) {
    unsigned value = [[object valueForKey:@"value"] unsignedIntValue];

    if (value < maxVal) {
        [matchedVals addObject:object];
    }
}

The former is obviously shorter and easier to understand. Are there any performance issues that should deter me from using that form versus the latter? This isn't about premature optimization or anything. It's just that we have this new technology that seems really cool and can obviously shorten your code. Is there a significant performance penalty involved?

Thanks for any insight.

-Prachi
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSPredicate performance considerations
      • From: mmalcolm crawford <email@hidden>
  • Prev by Date: Re: Modal dialog weirdness
  • Next by Date: Re: Problems making a framework (was tabbed class like Safari)
  • Previous by thread: Re: Thread context switch
  • Next by thread: Re: NSPredicate performance considerations
  • Index(es):
    • Date
    • Thread