三种遍历方式的区别

for VS for(... in ...)


for 的应用范围广基本可以NSArrayNSArray以及C语言的数组等,而for(... in ...)仅限于NSArrayNSArray

for(... in ...) 更简洁、效率更高

测试代码:


  10^7 的数组,时间单位 秒,精确度 毫秒


复制代码

NSMutableArray *test = [NSMutableArray array];

for (int i= 0; i < 10000000; i++) {

    [test addObject:@(i)];

}

int sum = 0;


double date_s = CFAbsoluteTimeGetCurrent();

for (int i = 0;i < test.count; i++) {

    sum += 1;

}

double date_e =  CFAbsoluteTimeGetCurrent();

NSLog(@"ForLoop Time: %f", date_e - date_s);


date_s =  CFAbsoluteTimeGetCurrent();

for (id obj in test) {

    sum += 1;

}

date_e =  CFAbsoluteTimeGetCurrent();

NSLog(@"Enumeration Time: %f", date_e - date_s);

复制代码



测试结果:




考虑到实际情况,ForLoop 的操作较多些。


测试代码:


硬件:i5 Cpu, 10G 内存,Mac OS X 10.9.4


数据量:10^7 的数组,


时间:单位 秒,精确度 毫秒


复制代码

NSMutableArray *test = [NSMutableArray array];

for (int i= 0; i < 10000000; i++) {

    [test addObject:@(i)];

}

int sum = 0;


double date_s = CFAbsoluteTimeGetCurrent();

for (int i = 0;i < test.count; i++) {

    int key = [test[i] intValue];

    sum += key;

    sum -= key;

}

double date_e =  CFAbsoluteTimeGetCurrent();

NSLog(@"ForLoop Time: %f", date_e - date_s);


date_s =  CFAbsoluteTimeGetCurrent();

for (id obj in test) {

    int key = [obj intValue];

    sum += key;

    sum -= key;

}

date_e =  CFAbsoluteTimeGetCurrent();

NSLog(@"Enumeration Time: %f", date_e - date_s);

复制代码

测试结果:






enumerateObjectsUsingBlock VS for(... in ...)


for(... in ...)用起来非常方便、简洁,同时 enumerateObjectsUsingBlock: 也有很多新特性:


通常enumerateObjectsUsingBlock: (for(... in ...)在效率上基本一致,有时会快些。主要是因为它们都是基于 NSFastEnumeration 实现的. 快速迭代在处理的过程中需要多一次转换,当然也会消耗掉一些时间. 基于Block的迭代可以达到本机存储一样快的遍历集合. 对于字典同样适用,而数组的迭代却不行。

                                 

                                 注意"enumerateObjectsUsingBlock" 修改局部变量时, 你需要声明局部变量为 __block 类型.

                                 

                                 enumerateObjectsWithOptions:usingBlock: 支持并发迭代或反向迭代,并发迭代时效率也非常高.

                                 

                                 对于字典而言, enumerateObjectsWithOptions:usingBlock 也是唯一的方式可以并发实现恢复Key-Value.

                                 

                                 就个人而言, 我偏向于使用 enumerateObjectsUsingBlock: 当然最后还是要根据实际情况上下文决定用什么

                                 

                                 测试代码:

                                 

                                 硬件:i5 Cpu, 10G 内存,Mac OS X 10.9.4

                                 

                                 数据量:10^4 的数组,执行一次NSLog输出

                                 

                                 时间:单位 秒,精确度 毫秒

                                 

                                 复制代码

                                 NSMutableArray *test = [NSMutableArray array];

                                 for (int i= 0; i < 10000; i++) {

                                     [test addObject:@(i)];

                                 }

                                 

                                 double date_s = CFAbsoluteTimeGetCurrent();

                                 for (id obj in test) {

                                     NSLog(@"%@",obj);

                                 }

                                 double date_e =  CFAbsoluteTimeGetCurrent();

                                 NSLog(@"ForLoop Time: %f", date_e - date_s);

                                 

                                 date_s =  CFAbsoluteTimeGetCurrent();

                                 //    [test enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

                                 //        NSLog(@"%@",obj);

                                 //    }];

                                 [test enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    NSLog(@"%@",obj);;

}];

                                 date_e =  CFAbsoluteTimeGetCurrent();

                                 NSLog(@"Enumeration Time: %f", date_e - date_s);

复制代码

测试结果:


// ForLoop Time: 14.951485

// Default Enumeration Time: 14.702673

// Reverse Enumeration Time: 14.948526

// Concurrent  Enumeration Time: 10.056317



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值