2012-11-21 6 views
1

NSCompoundPredicate를 초기화하는 데 사용되는 배열의 하위 술어 순서가 효율성에 영향을 줍니까?NSCompoundPredicate 하위 규칙 순서

예 : 예에서는

NSString * str = [self getAStr]; 
NSManagedObject * mo = [self getAMo]; 
NSString * fmt = @"(stringAttribute ==[cd] %@) AND (relationships CONTAINS %@)"; 
NSString * fmt2 = @"[email protected] != 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이 무시 될 것이라고 가정합니다.

답변

2

예. 평가 순서가 중요합니다. 항상 가장 효율적이고 제한적인 술어를 왼쪽에 넣으십시오 - 가능한 한 빨리 많은 것들을 실격하십시오. (술어는 실제로 비싸다는 것을 명심하십시오).

디버거를 중단하고 'cp'조건부를 인쇄하는 경우 왼쪽에서 오른쪽 방향으로 가능한 한 효율적인지 확인하십시오.