编译器开销:目标C中的IF条件需要多少开销?

时间:2013-04-10 07:24:49

标签: objective-c

请参阅以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";    
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                              reuseIdentifier:CellIdentifier];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
     }

 //... some other code 
 if (imageLoadingQueue == nil) {
    imageLoadingQueue = [[NSOperationQueue alloc] init];
    [imageLoadingQueue setName:@"Image Loading Queue"];
}
//... some other code 

return cell;
}

现在我们知道每次滚动时都会调用方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,但请参考条件:if (cell == nil)& if (imageLoadingQueue == nil)。再次检查这种情况。再一次。

所以我想知道如果条件成本会带来多少性能开销?

编辑:我们如何衡量?任何工具?

1 个答案:

答案 0 :(得分:4)

if声明本身可以忽略不计。 if语句中使用的条件可能很重要(如if (somethingThatTakesTenSeconds()) ...中所示),但在这种情况下不是这样,只需检查指针{ {1}}值。

无论如何,这几乎不重要。如果您需要选择是否发生了某些事情,那么 可以使用选择语句,无论是nilif还是三元操作。所以它有多繁重,并没有真正进入这个等式。