用于初始化NSCompoundPredicate的数组中的子谓词的顺序是否会影响效率?
示例:
NSString * str = [self getAStr];
NSManagedObject * mo = [self getAMo];
NSString * fmt = @"(stringAttribute ==[cd] %@) AND (relationships CONTAINS %@)";
NSString * fmt2 = @"relationships.@count != 0";
NSPredicate * p1 = [NSPredicate predicateWithFormat:fmt,str,mo];//expensive predicate
NSPredicate * p2 = [NSPredicate predicateWithFormat:fmt2,nil];//less expensive predicate
NSCompoundPredicate * cp = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1,p2];
[self fetchSomethingWithPredicate:cp];在上面的例子中,p1和p2的顺序是否改变了复合谓词的求值方式?显然,首先评估p2会更有效,假设如果p2评估为false,p1将被忽略。
发布于 2012-11-21 09:26:27
是的,评估的顺序很重要。总是把最有效和最有限的谓词放在左边-尽可能快地取消更多的东西(尽管要记住,有些谓词确实很昂贵)。
如果您中断调试器并打印出'cp‘谓词,请确保它从左到右尽可能高效。
https://stackoverflow.com/questions/13484547
复制相似问题