关于EnumerateObjectsUsingBlock和for-in之间的较量

关于EnumerateObjectsUsingBlock和for-in之间的较量

如果我们要遍历一个数组, 上过编程课程的童鞋都会想到For语句去循环.

Objective C 提供一个Block的遍历方法, 那么用它还是用For好呢?

下面我们去实践一下:

遍历一个数组看谁快

参赛选手ForLoop,For

- in,enumerateObjectsUsingBlock这个三个方法:

```

NSMutableArray*test = [NSMutableArrayarray];for(inti =0; i <1000000; i ++) {        [test addObject:@(i)];    }    __blockintsum =0;doubledate_s =CFAbsoluteTimeGetCurrent();for(inti =0; i < test.count; i ++) {        sum += [test[i] integerValue];    }doubledate_current =CFAbsoluteTimeGetCurrent() - date_s;NSLog(@"Sum : %d ForLoop Time: %f ms",sum,date_current *1000);    sum =0;    date_s =CFAbsoluteTimeGetCurrent();for(NSNumber*numintest) {        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:^(idobj,NSUIntegeridx,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 - in,enumerateObjectsUsingBlock,enumerateObjectsWithOptions这个三个方法: (ForLoop已经不再继续讨论了)

NSMutableArray*test = [NSMutableArrayarray];for(inti =0; i <10000000; i ++) {        [test addObject:@(i +10)];    }//For-in__blockNSIntegerindex =0;doubledate_s =CFAbsoluteTimeGetCurrent();for(NSNumber*numintest) {if([num integerValue] ==9999999) {            index = [test indexOfObject:num];break;        }    }doubledate_current =CFAbsoluteTimeGetCurrent() - date_s;NSLog(@"index : %ld For-in Time: %f ms",(long)index,date_current *1000);//enumerateObjectsUsingBlockindex =0;    date_s =CFAbsoluteTimeGetCurrent();    [test enumerateObjectsUsingBlock:^(idnum,NSUIntegeridx,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);//enumerateObjectsWithOptionsindex =0;    date_s =CFAbsoluteTimeGetCurrent();    [test enumerateObjectsWithOptions:NSEnumerationConcurrentusingBlock:^(idnum,NSUIntegeridx,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-in和enumerateObjectsWithOptions方法这里我比较喜欢第二种写法简洁直观.

现在咱们要遍历字典

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

NSDictionary*testDictionary = @{@"Auther":@"yyyyy",@"Game":@"Dota",@"App":@"dddddd",@"Market":@"AppStore"};//For - inNSMutableArray*forInArry = [NSMutableArrayarray];doubledate_s =CFAbsoluteTimeGetCurrent();NSArray*keys = [testDictionary  allKeys];for(NSString*keyinkeys) {NSString*Value = testDictionary[key];        [forInArry addObject:Value];    }doubledate_current =CFAbsoluteTimeGetCurrent() - date_s;NSLog(@"index : %ld For-in Time: %f ms",(long)index,date_current *1000);//enumerateKeysAndObjectsUsingBlockdate_s =CFAbsoluteTimeGetCurrent();NSMutableArray*enumArry = [NSMutableArrayarray];    [testDictionary enumerateKeysAndObjectsUsingBlock:^(idkey,idobj,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);

打印输出:

关于EnumerateObjectsUsingBlock和for-in之间的较量_第1张图片

打印输出

enumerateKeysAndObjectsUsingBlock胜出

结论:

当我们想遍历字典类型的时候, 推荐使用enumerateKeysAndObjectsUsingBlock

不仅仅是因为速度快, 更是因为代码更优雅和直观.



http://blog.csdn.net/scboyhj__/article/details/51397104

转自这里

你可能感兴趣的:(关于EnumerateObjectsUsingBlock和for-in之间的较量)