美文网首页
遍历方法功能和效率分析(数组、字典、集合)

遍历方法功能和效率分析(数组、字典、集合)

作者: Qing学 | 来源:发表于2017-11-11 14:14 被阅读0次

    一、数组遍历:
    1.常用for

    NSMutableArray *testArray = [NSMutableArray array];
        for (int i = 0; i < 100000; i++){
            [testArray addObject:@(i)];
        }
        __block int sum = 0;
        double date_for = CFAbsoluteTimeGetCurrent();
        for (int i = 0; i < testArray.count; i++){
            NSNumber *number = testArray[i];
            sum = [number intValue] + sum;
        }
        double date_forEnd = CFAbsoluteTimeGetCurrent();
        double date_forUse = date_forEnd - date_for;
        NSLog(@"sum = %d  forUseTime = %lf",sum,date_forUse);
    

    结论for的计算时间为

    2017-11-11 13:29:03.507891+0800 checkAllThing[4443:89434] sum = 704982704  forUseTime = 0.003164
    

    2.使用for in进行遍历计算

    sum = 0;
        for (NSNumber *num in testArray){
            int number = [num intValue];
            sum += number;
        }
        double date_forInEnd = CFAbsoluteTimeGetCurrent();
        double date_forInUse = date_forInEnd - date_forEnd;
        NSLog(@"sum = %d    ForInTimeUse = %f",sum,date_forInUse);
    

    for in进行遍历的结果为

    2017-11-11 13:29:03.510339+0800 checkAllThing[4443:89434] sum = 704982704    ForInTimeUse = 0.002497
    

    3.使用enumerateObjectsUsingBlock进行计算

    sum = 0;
        [testArray enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            sum += [obj intValue];
        }];
        double dateEnumerateObjectNoOptionEnd = CFAbsoluteTimeGetCurrent();
        double dateEnumerateObjectNoOptionUse = dateEnumerateObjectNoOptionEnd - date_forInEnd;
        NSLog(@"sum = %d   EnumerateObjectNoOption = %f",sum,dateEnumerateObjectNoOptionUse);
    

    计算结果为

    2017-11-11 13:29:03.515483+0800 checkAllThing[4443:89434] sum = 704982704   EnumerateObjectNoOption = 0.005145
    

    4.enumerateObjectsWithOptions的计算

    sum = 0;
        [testArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSNumber * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            sum += [obj intValue];
        }];
        double dateEnumerateOption = CFAbsoluteTimeGetCurrent();
        double dateEnumerateOptionUse = dateEnumerateOption - dateEnumerateObjectNoOptionEnd;
        NSLog(@"sum = %d   EnumerateObjectNoOptionEnd = %f",sum,dateEnumerateOptionUse);
    

    计算结果为

    2017-11-11 13:29:03.520126+0800 checkAllThing[4443:89434] sum = -1728666209   EnumerateObjectNoOptionEnd = 0.004646
    

    5.使用RAC流进行遍历

    sum = 0;
        NSArray *arrayData = [[testArray.rac_sequence map:^id(NSNumber * value) {
            sum += [value intValue];
            return value;
        }]array];
        double dateRac_sequence = CFAbsoluteTimeGetCurrent();
        double dateRac_sequenceUse = dateRac_sequence - dateEnumerateOption;
        NSLog(@"sum = %d rac_sequence = %f",sum,dateRac_sequenceUse);
    

    计算结果为:

    2017-11-11 13:29:03.708944+0800 checkAllThing[4443:89434] sum = 704982704 rac_sequence = 0.188814
    

    结论分析

    2017-11-11 13:29:03.507891+0800 checkAllThing[4443:89434] sum = 704982704  forUseTime = 0.003164
    2017-11-11 13:29:03.510339+0800 checkAllThing[4443:89434] sum = 704982704    ForInTimeUse = 0.002497
    2017-11-11 13:29:03.515483+0800 checkAllThing[4443:89434] sum = 704982704   EnumerateObjectNoOption = 0.005145
    2017-11-11 13:29:03.520126+0800 checkAllThing[4443:89434] sum = -1728666209   EnumerateObjectNoOptionEnd = 0.004646
    2017-11-11 13:29:03.708944+0800 checkAllThing[4443:89434] sum = 704982704 rac_sequence = 0.188814
    

    通过结果分析得出以下结论
    1.for in 的效率最高,但是在遍历过程中不能实时取得value所对应的index值
    2.如果要通过value查询index可以使用enumerateObjectsWithOptions,使用此方法遍历可以使用多线程遍历。但是遍历过程可以无序。效率最高。(用到了dispatch group)
    3.如果要通过遍历。通过一定的条件筛选得到一些数据。可以使用rac_sequence方法。直接获得。
    4.要想有序的遍历可以使用enumerateObjectsUsingBlock更加清晰直观。但是比for稍微慢一点。

    二、字典遍历
    1.利用allKeys进行遍历

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        for (int i = 0; i < 10000; i++){
            [dic setObject:@(i) forKey:@(i)];
        }
        
        double allKeyStart = CFAbsoluteTimeGetCurrent();
        for (NSNumber * num in [dic allKeys]){
            NSNumber *num = num;
        }
        double allKeyEnd = CFAbsoluteTimeGetCurrent();
        double allKeyUse = allKeyEnd - allKeyStart;
        NSLog(@"allKeyUseTime = %f",allKeyUse);
    
    运行结果为:2017-11-11 14:02:41.185373+0800 checkAllThing[4939:109563] allKeyUseTime = 0.000173
    

    2.利用allValues

    for (NSNumber *numValue in [dic allValues]){
            NSNumber *num = numValue;
        }
        double dateAllValueEnd = CFAbsoluteTimeGetCurrent();
        double dateAllValueUse = dateAllValueEnd - allKeyEnd;
        NSLog(@"dateAllValueEnd = %f",dateAllValueUse);
        
        [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            NSNumber *num = obj;
        }];
        double dateEnumerateNoOptionEnd = CFAbsoluteTimeGetCurrent();
        double dateEnumerationNoOptionUse = dateEnumerateNoOptionEnd - dateAllValueEnd;
        NSLog(@"dateEnumerateNoOptionUse = %f",dateEnumerationNoOptionUse);
    

    运行结果为:

    2017-11-11 14:02:41.185776+0800 checkAllThing[4939:109563] dateAllValueEnd = 0.000449
    

    3.使用enumerateKeysAndObjectsWithOptions进行遍历

    [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            NSNumber *num = obj;
        }];
        double dateEnumerateNoOptionEnd = CFAbsoluteTimeGetCurrent();
        double dateEnumerationNoOptionUse = dateEnumerateNoOptionEnd - dateAllValueEnd;
        NSLog(@"dateEnumerateNoOptionUse = %f",dateEnumerationNoOptionUse);
    

    运行结果为

    2017-11-11 14:02:41.186737+0800 checkAllThing[4939:109563] dateEnumerateNoOptionUse = 0.000957
    

    4.使用enumerateKeysAndObjectsWithOptions进行运行

    [dic enumerateKeysAndObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            NSNumber *num = obj;
        }];
        double dateEnumerateOptionEnd = CFAbsoluteTimeGetCurrent();
        double dateEnumerationOptionUse = dateEnumerateOptionEnd - dateEnumerateNoOptionEnd;
        NSLog(@"dateEnumrateOptionUse%f",dateEnumerationOptionUse);
    

    运行结果为

    2017-11-11 14:02:41.187683+0800 checkAllThing[4939:109563] dateEnumrateOptionUse0.000947
    

    运行结果进行总结分析如下

    2017-11-11 14:02:41.185373+0800 checkAllThing[4939:109563] allKeyUseTime = 0.000173
    2017-11-11 14:02:41.185776+0800 checkAllThing[4939:109563] dateAllValueEnd = 0.000449
    2017-11-11 14:02:41.186737+0800 checkAllThing[4939:109563] dateEnumerateNoOptionUse = 0.000957
    2017-11-11 14:02:41.187683+0800 checkAllThing[4939:109563] dateEnumrateOptionUse0.000947
    

    1.运行分析发现使用allKeys进行遍历效率最高。
    2.通过enumrateOption方法可以清晰直观的查看key和value值。
    三、集合处理

    NSSet *set = [NSSet setWithObjects:@"12",@"32",@"2",@"12", nil];
        [set enumerateObjectsUsingBlock:^(id  _Nonnull obj, BOOL * _Nonnull stop) {
            NSLog(@"%@",obj);
        }];
    

    相关文章

      网友评论

          本文标题:遍历方法功能和效率分析(数组、字典、集合)

          本文链接:https://www.haomeiwen.com/subject/bwswmxtx.html