enumerateObjectsUsingBlock 、for 、for(... in ...) 的差别 & 性能測试

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

  1. for 的应用范围广基本能够NSArray、NSArray以及C语言的数组等,而for(... in ...)仅限于NSArray、NSArray等
  2. 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

參考:

http://stackoverflow.com/questions/4486622/when-to-use-enumerateobjectsusingblock-vs-for


原文地址:https://www.cnblogs.com/gcczhongduan/p/5248135.html