看<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
相关推荐
详细的说明和用法 NSArray,NSSet,NSDictionary 以及他们子类的说明
object-c基础语法NSSet集合, 适合初学者,主要学习集合NSSet的设置、判断集合中是否包含某个对象、快速枚举遍历、判断集合是否相等,可变集合的操作等
一些NSArray,NSDictionary,NSSet相关的算法知识1
NSArray,NSSet,NSEnumerator,NSDictionary及对应Mutable介绍,有对应的实例代码
这是NSArray,NSSet,NSEnumerator,NSDictionary及对应Mutable介绍的示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7884218
主要介绍了iOS集合遍历(NSArray、NSDictionary、NSSet)的方法,文中给出了详细的方法示例,并总结了各个方法的优缺点来供大家学习参考,需要的朋友们下面来一起看看吧。
NSSet NSString NSTimer NSURL UIKit UIBezierPath UIButton UIColor UIDevice UIImage UIImageView UILable UINavigationController UIResponder UIScrollView UISearchBar UITableViewCell UITextField ...
NSSet、NSMutableSet、NSOrderedSet、NSMutableOrderedSet了解与学习
.4jecCiEe C语法R-11eCr-1,DC34le1eC魏先宇的程序人生魏先宇的程序人生多读M多bM多思M方有多得!NSEnumerator *enum
(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ NSSet *touche = touches; UITouch *touch = [touche anyObject]; self.magnifyView.magnPoint = [touch locationInView:self.view];//主要代码 ...
前言 iPad 会发生闪退 ,然而无法在iPhone重现。 最终定位是因为执行了一些废弃的代码。这代码里面将一个空的按钮对象设置图片。 导致这样的问题,是因为之前的同事代码删除的只是删除了一半,注释了赋值代码,却...
在撰写本文时,访问器正在以常规的无序KVC访问器模式( NSSet )自动生成,从而导致运行时崩溃。 除了大多数访问器的无法识别的选择器之外,以下是常见的例外情况: *** Terminating app due to uncaught exception...
文档中包括: NSNumber,NSArray,NSString,NSDictionary,NSSet 属性以及相关方法
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; currentPoint = [touch locationInView:self]; NSLog(@"currentPoint.x = %f",currentPoint.x); ...
例如,要查找标签为foo频道: [MMXChannel findByTags:[NSSet setWithArray:@[@"foo"]]] .then(^(NSNumber *count, NSArray* channels){ });对于在执行回调之前返回messageID消息方法,将以messageID作为第一个参数...
但是,苹果对于我们的NSDictionary、NSSet、NSArray等值有中文时,打印出来的是Unicode编码,人类无法直接读懂,因此,笔者研究研究如何将打印出来的日志保持原有的格式化且能够将Unicode编码打印出来是正常人类可读...
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch * touch=[touches anyObject]; currentPoint =[touch locationInView:self]; UIGraphicsBeginImageContext(self.frame.size); ...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet * dataSet = [[ NSSet alloc ] initWithObjects: @" com.comquas.iap.test " , nil ]; [IAPShare sharedHelper ]. iap = [[IAPHelper alloc ] initWithProductIdentifiers: dataSet]; } 生产模式开/关 ...