• 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

     
     
     
  • 相关阅读:
    select2 下拉搜索 可编辑可搜索 / 只可搜索
    获取服务器时间ajax
    table中td 内容超长 自动折行 (含字母数字文字)
    下拉菜单 ,三级联动 ,夹其它下拉菜单
    echarts 添加标线,设置颜色
    table 中的tr 行点击 变换颜色背景
    checkebox 全选 ,子复选框单个全部选择后,全选框也会被选择
    replace替换,全局和局部替换
    字断行
    ydoc 参考系列
  • 原文地址:https://www.cnblogs.com/skyming/p/3977075.html
Copyright © 2020-2023  润新知