0
-(NSPredicate *) predicateForExactMatch
{
    enum typeOfAutocompleteNew toc = [[self class] typeOfAutoComplete];
    NSPredicate * predictNameMatch=[NSPredicate predicateWithFormat:@"%K==[cd]%@",NSStringFromSelector(@selector(strNameofPlace)),self.strCurrentKeyword];
    NSPredicate * entryTypeNameMatch=[NSPredicate predicateWithFormat:@"%K==[cd]%@",NSStringFromSelector(@selector(strEntry)),@(toc)];
    NSPredicate * final = [NSCompoundPredicate andPredicateWithSubpredicates:@[predictNameMatch,entryTypeNameMatch]];
    return final;
}

哪一个更好?

    NSPredicate * final = [NSCompoundPredicate andPredicateWithSubpredicates:@[predictNameMatch,entryTypeNameMatch]];

或者

    NSPredicate * final = [NSCompoundPredicate andPredicateWithSubpredicates:@[entryTypeNameMatch,predictNameMatch]];

在这里,predictNameMatch 肯定要严格得多。最多1-2个是完全匹配的。entryTypeNameMatch 匹配 25% 的数据。

4

3 回答 3

2

如果重要,请对其进行测试和测量。

我怀疑一般来说 Martin R是对的,但性能优化是一门黑暗的艺术,唯一确定的方法是找出......

于 2013-09-27T08:55:09.407 回答
1

我不能给你确切的参考,因为我是在电话上写这篇文章的,但我很确定在某些 WWDC 核心数据会议上建议将最严格的谓词放在首位。

于 2013-09-27T08:50:25.843 回答
1

最严格的谓词应该放在第一位。在像这样的复合谓词中,谓词从左到右进行评估,一旦知道结果,处理就结束。在您的情况下,您有一个逻辑 AND,因此如果第一个谓词为假,则将跳过第二个谓词(因为它是不必要的)。因此,如果第一个谓词几乎没有任何匹配项,您几乎不需要评估第二个谓词。

这与条件语句中发生的快捷方式完全相同if。如果您使用if (a && b), 并且a为假,则b不会被评估。

于 2013-09-27T17:31:08.130 回答