• NSArray其中的方法--遍历,


    1. ForLoopFor - inenumerateObjects这个三个方法的区别:

    遍历一个数组用For-in最快.

    通过Value查询index的时候, 面对大量的数组推荐使用 enumerateObjectsWithOptions的并行方法.

    遍历字典类型的时候, enumerateKeysAndObjectsUsingBlock效率最高

      1.1遍历数组

        
        NSMutableArray *test = [NSMutableArray array];
        for (int i = 0; i < 1000000; i ++) {
            [test addObject:@(i)];
        }
        
        //ForLoop方法
        __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);
        
        //For - in方法
        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);
        
        //enumerateObjectsUsingBlock方法
        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最快.

      1.2-通过Value查找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);

    打印:

      

    结论:通过Value查询index的时候, 面对大量的数组推荐使用 enumerateObjectsWithOptions的并行方法.

      1.3遍历字典

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

        NSDictionary *testDictionary = @{
                                         @"Auther" : @"yyyyy",
                                         @"Game" : @"Dota",
                                         @"App" : @"dddddd",
                                         @"Market" : @"AppStore"
                                         };
        
        NSMutableArray *forInArry1 = [NSMutableArray array];
        NSMutableArray *forInArry2 = [NSMutableArray array];
        NSMutableArray *enumArry = [NSMutableArray array];
        
        
        //For - in方法 直接allValues
        double date_s = CFAbsoluteTimeGetCurrent();
        NSArray *values = testDictionary.allValues;
        for (NSString *value in values) {
            [forInArry1 addObject:value];
        }
        double date_current = CFAbsoluteTimeGetCurrent() - date_s;
        NSLog(@"index : %ld For-in-forInArry1 Time: %f ms",(long)index,date_current * 1000);
        
        //For - in方法,+根据key取value值
        date_s = CFAbsoluteTimeGetCurrent();
        NSArray *keys = testDictionary.allKeys;
        for (NSString *key in keys) {
                    NSString *Value = testDictionary[key];
            [forInArry2 addObject:Value];
        }
        date_current = CFAbsoluteTimeGetCurrent() - date_s;
        NSLog(@"index : %ld For-in-forInArry2 Time: %f ms",(long)index,date_current * 1000);
        
        //enumerateKeysAndObjectsUsingBlock方法.
        date_s = CFAbsoluteTimeGetCurrent();
        [testDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            [enumArry addObject:obj];
        }];
        date_current = CFAbsoluteTimeGetCurrent() - date_s;
        NSLog(@"index : %ld enumerateKeysAndObjectsUsingBlock Time: %f ms",(long)index,date_current * 1000);
        

    打印:

    结论:遍历字典类型的时候, enumerateKeysAndObjectsUsingBlock效率最高

  • 相关阅读:
    arcgis for silverlight 控制图层显示级别
    Telerik for silverlight RadAutoCompleteBox 动态数据源
    ARM嵌入式学习--OK6410裸板程序--2.GPIO控制LED跑马灯(从ARM汇编跳转到C语言)
    ARM嵌入式学习--OK6410裸板程序--1.GPIO控制LED
    Linux内核移植--1.添加NAND Flash分区
    Linux 快速释放端口与释放内存缓存
    jquery ajax session超时处理
    相濡以沫不如相忘江湖
    SQL Server数据库无法启动(万金油解决办法)
    多显示器实现屏幕扩展(VGA DVI HDMI)
  • 原文地址:https://www.cnblogs.com/jiayongqiang/p/5596154.html
Copyright © 2020-2023  润新知