美文网首页
教你用正确的姿势遍历OC中的collection

教你用正确的姿势遍历OC中的collection

作者: 农村高富帅 | 来源:发表于2016-12-20 00:41 被阅读0次

引言

无论那种语言,我们都会大量的去操作collection(集合),对collection不停的做增删该查,OC也不例外。而当我们对collection做操作的时候应该时刻关注操作复杂度,包括时间复杂度和空间复杂度,使自己的代码用尽可能低的内存消耗和尽可能少的时间来完成需求。本文旨在以“黑盒”的方式来比较OC中用不同的方法遍历NSMutableArray和NSMutableDictionary中的值所用的时间。

环境

  • MACBook Pro 2.7 GHz Intel Core i5
  • MAC OS X 10.11.6 (15G1004)
  • XCode Version 8.2 (8C38)

步骤

1,创建ZMTest工程,在ViewController中定义如下属性

@property (nonatomic, strong) NSMutableArray *testArray;
@property (nonatomic, strong) NSMutableDictionary *testDictionary;

2,初始化

self.testArray = [NSMutableArray new];
self.testDictionary = [NSMutableDictionary new];

for (int i = 0; i <= 10000000; i ++) {
    NSString *value = [NSString stringWithFormat:@"%d",i];
    [self.testArray addObject:value];
    [self.testDictionary setValue:value forKey:value];
}

3,引入获取代码运行时间的宏(这是公司一个大神写的,地址

/** 快速查询一段代码的执行时间 */
/** 用法
TICK
do your work here
TOCK
*/
#define TICK NSDate *startTime = [NSDate date];
#define TOCK NSLog(@"Time:%f\n", -[startTime timeIntervalSinceNow]);

4,实现如下方法

- (void)testArraty1{
TICK

for (int i = 0; i < self.testArray.count; i ++) {
    if ([self.testArray[i] isEqualToString:@"10000000"]) {
        NSLog(@"testArraty1");
    }
}
TOCK
}

- (void)testArraty2{
TICK
NSInteger *index = 0;
for (NSString *value in self.testArray) {
    index  = index + 1;
    if ([value isEqualToString:@"10000000"]) {
        NSLog(@"testArraty2");
    }
}
TOCK
}

- (void)testArraty3{
  TICK
  [self.testArray enumerateObjectsUsingBlock:^(NSString * value, NSUInteger i, BOOL * _Nonnull stop) {
      if ([value isEqualToString:@"10000000"]) {
          NSLog(@"testArraty3");
      }
  }];
  TOCK
  }

- (void)testDictionary1{
TICK
[self.testDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL * _Nonnull stop) {
    if ([key isEqualToString:@"10000000"]) {
        NSLog(@"testDictionary1");
    }
}];
TOCK
}

- (void)testDictionary2{
TICK

NSArray *allKey = [self.testDictionary allKeys];
for (NSString * key in allKey) {
    NSString *value = self.testDictionary[key];
    if ([key isEqualToString:@"10000000"]) {
        NSLog(@"testDictionary2");
    }
}

TOCK
}

然后再viewDidLoad方法中调用这几个方法

5,运行,结果如下

Paste_Image.png

以下是整理的表格

方法 耗时 说明
testArraty1 1.530919 Execute code for each value in a sequence of values(不会翻译😭,就是最简单的for循环)
testArraty2 0.131923 NSFastEnumeration协议方法
testArraty3 0.489827 基于块的遍历
testDictionary1 0.674507 基于块的遍历
testDictionary2 2.830808 通过allKey是方法获取所有的key,然后通过key获取value

6,结论

(1)在遍历NSArray的时候最好使用NSFastEnumeration协议的这种方式(for...in...),效率明显高于通过定义索引然后检索的方式,当然为了操作简单,也可以使用基于块的方式来遍历,基于block的方式遍历的好处也显而易见,直接可以获取到下标、值和跳出循环的BOOL值,当然该方式的的耗时也高于NSFastEnumeration协议的方式,不过,我觉得可以接受。

(2)在遍历NSDictionary的时候,请务必使用基于block的形式,不仅大量的降低时耗,而且能获取到key、value和跳出循环的BOOL值。可谓方便快捷。

最后

接触的新东西的越多。越觉得自己欠缺,最近在看《编写高质量IOS与OS X代码的52个有效方法》一书,真心推荐。而本文也算是书中一个章节的读后感。

相关文章

网友评论

      本文标题:教你用正确的姿势遍历OC中的collection

      本文链接:https://www.haomeiwen.com/subject/ktkxvttx.html