NSSet的查找

    看<Pro.iOS.Apps.Performance.Optimization>那本书偶然发现^^NSSet查找算法竟然是O(1).

   

    首先当然是读数据进NSSet(这的耗时不论),set也能放自定义data(测试用的MyItem包含identifier和name属性)

    然后是重写MyItem的isEqual和hash,因为查找时会调用isEqual,至于hash,见hash的文档描述:

If two objects are equal (as determined by the isEqual: method), they must have the same hash value. This last point is particularly important if you define hash in a subclass and intend to put instances of that subclass into a collection.

 

    根据哪个property查找就相应地改写(这用的identifier)

- (BOOL)isEqual:(id)anObject {
    if (anObject == self) {
        return YES;
    }
    
    if (![anObject isKindOfClass:[self class]]) {
        return NO;
    }
    
    MyItem *item = (MyItem *)anObject;
    if ([self.identifier isEqual:item.identifier]) {
        return YES;
    }
    
    return NO;
}


- (NSUInteger)hash {
    return [_identifier hash];
}

 

    测试代码:

NSMutableArray *itemArray = [NSMutableArray array];
    long itemCount = 100000;
    for (int i = 0; i < itemCount; i ++) {
        @autoreleasepool {
            MyItem *item = [[[MyItem alloc] initWithIdentifier:[NSString stringWithFormat:@"%d",i + 1]] autorelease];
            [itemArray addObject:item];
        }
    }

    NSMutableSet *set = [NSMutableSet setWithArray:itemArray];
    
    MyItem *newItem = [[MyItem alloc] initWithIdentifier:[NSString stringWithFormat:@"%d",100000]];
    NSLog(@"search for item %@ identifier %@",newItem,newItem.identifier);

    MyItem *oldItem = nil;
    NSDate *date = [NSDate date];
    
    for (MyItem *item in itemArray) {
        if ([item isEqual:newItem]) {
            oldItem = item;
            break;
        }
    }
    
    NSLog(@"for 耗费时间 : %f s",[[NSDate date] timeIntervalSinceDate:date]);
    NSLog(@"old item %@ identifier %@",oldItem,oldItem.identifier);

    oldItem = nil;
    date = [NSDate date];
    
    oldItem = [set member:newItem];
    
    NSLog(@"set 耗费时间 : %f s",[[NSDate date] timeIntervalSinceDate:date]);
    NSLog(@"old item %@ identifier %@",oldItem,oldItem.identifier);

 

 输出:

 

10万,速度上绝对秒杀^^

2013-04-12 10:14:19.730 BlockTest[1088:903] search for item <MyItem: 0x1001050d0> identifier 100000

2013-04-12 10:14:19.742 BlockTest[1088:903] for 耗费时间 : 0.010545 s

2013-04-12 10:14:19.743 BlockTest[1088:903] old item <MyItem: 0x1020406a0> identifier 100000

2013-04-12 10:14:19.744 BlockTest[1088:903] set 耗费时间 : 0.000009 s

2013-04-12 10:14:19.745 BlockTest[1088:903] old item <MyItem: 0x1020406a0> identifier 100000

 

100万(for查找时间正好10倍,说明就是O(n),而set查找几乎不变,O(1))

2013-04-12 10:23:55.652 BlockTest[1249:903] search for item <MyItem: 0x1001050d0> identifier 1000000

2013-04-12 10:23:55.753 BlockTest[1249:903] for 耗费时间 : 0.100014 s

2013-04-12 10:23:55.754 BlockTest[1249:903] old item <MyItem: 0x105c1d7e0> identifier 1000000

2013-04-12 10:23:55.754 BlockTest[1249:903] set 耗费时间 : 0.000006 s

2013-04-12 10:23:55.755 BlockTest[1249:903] old item <MyItem: 0x105c1d7e0> identifier 1000000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值