關於EnumerateObjectsUsingBlock和for-in之間的較量

遍歷一個數組看誰快

參賽選手ForLoop,For - in,enumerateObjectsUsingBlock這個三個方法: 數組

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);

最後輸出以下: oop


打印輸出
  • For-in方法最快速

    結論:

    當只是遍歷一個數組的時候使用For-in會比較快速, 推薦使用For-in遍歷數組.

經過Value查找Index看誰快

假如如今咱們要查找一個Value, 這個Value 值是100001, 找出它的index (數組的序列號). spa

那麼如今咱們來比較一下
參賽選手For - in,enumerateObjectsUsingBlock,enumerateObjectsWithOptions這個三個方法: (ForLoop已經再也不繼續討論了) code

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);

最後輸出以下圖: it


打印輸出
  • enumerateObjectsWithOptions方法最快速

    結論:

    經過Value查詢index的時候, 面對大量的數組推薦使用enumerateObjectsWithOptions的並行方法.
    For-in和enumerateObjectsWithOptions方法這裏我比較喜歡第二種寫法簡潔直觀.

如今我們要遍歷字典

這裏咱們比較一下使用For-in和enumerateKeysAndObjectsUsingBlock這個兩個方法: io

NSDictionary *testDictionary = @{ @"Auther" : @"yyyyy", @"Game" : @"Dota", @"App" : @"dddddd", @"Market" : @"AppStore" }; //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 : %ld For-in Time: %f ms",(long)index,date_current * 1000); NSLog(@"ForInArr: %@",forInArry); NSLog(@"enumArry: %@",enumArry);

打印輸出: table


打印輸出
  • enumerateKeysAndObjectsUsingBlock勝出

    結論:

    當咱們想遍歷字典類型的時候, 推薦使用enumerateKeysAndObjectsUsingBlock
    不單單是由於速度快, 更是由於代碼更優雅和直觀.


文/南國青天(簡書做者) 原文連接:http://www.jianshu.com/p/ef3f1731a353 著做權歸做者全部,轉載請聯繫做者得到受權,並標註「簡書做者」。
相關文章
相關標籤/搜索