NSPredicate performance considerations
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