zoukankan      html  css  js  c++  java
  • 关于EnumerateObjectsUsingBlock和for-in之间的较量

    原文:http://www.jianshu.com/p/ef3f1731a353

    如果我们要遍历一个数组, 上过编程课程的童鞋都会想到For语句去循环.
    Objective C 提供一个Block的遍历方法, 那么用它还是用For好呢?

    遍历一个数组看谁快

    参赛选手 ForLoopFor - inenumerateObjectsUsingBlock这个三个方法:

     NSMutableArray *test = [NSMutableArray array];
        for (int i = 0; i < 1000000; i ++) {
            [test addObject:@(i)];
        }
    
        __block int sum = 0;
        double date_s = CFAbsoluteTimeGetCurrent();
        for (int i = 0; i < test.count; i ++) {
            sum += [test[i] integerValue];
        }
        double date_current = CFAbsoluteTimeGetCurrent() - date_s;
        NSLog(@"Sum : %d ForLoop Time: %f ms",sum,date_current * 1000);
    
    
        sum = 0;
        date_s = CFAbsoluteTimeGetCurrent();
        for (NSNumber *num in test) {
            sum += [num integerValue];
        }
        date_current = CFAbsoluteTimeGetCurrent() - date_s;
        NSLog(@"Sum : %d For-in Time: %f ms",sum,date_current * 1000);
    
    
        sum = 0;
        date_s = CFAbsoluteTimeGetCurrent();
        [test enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            sum += [obj integerValue];
        }];
        date_current = CFAbsoluteTimeGetCurrent() - date_s;
        NSLog(@"Sum : %d enumrateBlock Time: %f ms",sum,date_current * 1000);


    最后输出如下:


     
    • For-in方法最快速

      结论:

      当只是遍历一个数组的时候使用For-in会比较快速, 推荐使用For-in遍历数组.

    通过Value查找Index看谁快

    假如现在我们要查找一个Value, 这个Value 值是100001, 找出它的index (数组的序列号).

    那么现在我们来比较一下
    参赛选手 For - inenumerateObjectsUsingBlockenumerateObjectsWithOptions 这个三个方法: (ForLoop已经不再继续讨论了)

        NSMutableArray *test = [NSMutableArray array];
        for (int i = 0; i < 10000000; i ++) {
            [test addObject:@(i + 10)];
        }
    
        //For-in
        __block NSInteger index = 0;
        double date_s = CFAbsoluteTimeGetCurrent();
        for (NSNumber *num in test) {
            if ([num integerValue] == 9999999) {
                index = [test indexOfObject:num];
                break;
            }
        }
        double date_current = CFAbsoluteTimeGetCurrent() - date_s;
        NSLog(@"index : %ld For-in Time: %f ms",(long)index,date_current * 1000);
    
        //enumerateObjectsUsingBlock
        index = 0;
        date_s = CFAbsoluteTimeGetCurrent();
        [test enumerateObjectsUsingBlock:^(id num, NSUInteger idx, BOOL *stop) {
            if ([num integerValue] == 9999999) {
                index = idx;
                *stop = YES;
            }
        }];
        date_current = CFAbsoluteTimeGetCurrent() - date_s;
        NSLog(@"index : %ld enumerateBlock Time: %f ms",(long)index,date_current * 1000);
    
        //enumerateObjectsWithOptions
        index = 0;
        date_s = CFAbsoluteTimeGetCurrent();
        [test enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id num, NSUInteger idx, BOOL *stop) {
            if ([num integerValue] == 9999999) {
                index = idx;
                *stop = YES;
            }
        }];
        date_current = CFAbsoluteTimeGetCurrent() - date_s;
        NSLog(@"index : %ld enumerateObjectsWithOptions Time: %f ms",(long)index,date_current * 1000);

    最后输出如下图:


     
    • enumerateObjectsWithOptions方法最快速

      结论:

      通过Value查询index的时候, 面对大量的数组推荐使用 enumerateObjectsWithOptions的并行方法.
      For-inenumerateObjectsWithOptions方法这里我比较喜欢第二种写法简洁直观.

    现在咱们要遍历字典

    这里我们比较一下使用 For-in 和 enumerateKeysAndObjectsUsingBlock 这个两个方法:

        NSDictionary *testDictionary = @{
                                         @"Auther" : @"糖醋排骨",
                                         @"blog" : @"http://www.cnblogs.com/tate-zwt/",
                                         @"name" : @"zwt"}; 
     //For - in
        NSMutableArray *forInArry = [NSMutableArray array];
        double date_s = CFAbsoluteTimeGetCurrent();
        NSArray *keys = [testDictionary  allKeys];
        for (NSString *key in keys) {
            NSString *Value = testDictionary[key];
            [forInArry addObject:Value];
        }
        double date_current = CFAbsoluteTimeGetCurrent() - date_s;
        NSLog(@"index : %ld For-in Time: %f ms",(long)index,date_current * 1000);
    
        //enumerateKeysAndObjectsUsingBlock
        date_s = CFAbsoluteTimeGetCurrent();
        NSMutableArray *enumArry = [NSMutableArray array];
        [testDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            [enumArry addObject:obj];
        }];
        date_current = CFAbsoluteTimeGetCurrent() - date_s;
        NSLog(@"index : %ldenumerateKeysAndObjectsUsingBlockTime: %f ms",(long)index,date_current * 1000); NSLog(@"ForInArr: %@",forInArry); NSLog(@"enumArry: %@",enumArry);

    最后输出如下图:


     
      • enumerateKeysAndObjectsUsingBlock胜出

        结论:

        当我们想遍历字典类型的时候, 推荐使用enumerateKeysAndObjectsUsingBlock
        不仅仅是因为速度快, 更是因为代码更优雅和直观.
     
  • 相关阅读:
    WCF中关于可靠会话的BUG!!
    控制并发访问的三道屏障: WCF限流(Throttling)体系探秘[下篇]
    《天使之恋》,一部重口味的纯美爱情电影
    一个关于解决序列化问题的编程技巧
    [转]Design Rules for ModelViewPresenter
    你知道Unity IoC Container是如何创建对象的吗?
    只在UnitTest和WebHost中的出现的关于LogicalCallContext的严重问题
    使命必达: 深入剖析WCF的可靠会话[原理揭秘篇](上)
    回调与并发: 通过实例剖析WCF基于ConcurrencyMode.Reentrant模式下的并发控制机制
    如何编写没有Try/Catch的程序
  • 原文地址:https://www.cnblogs.com/tate-zwt/p/5006043.html
Copyright © 2011-2022 走看看