测试代码以简单输出数字文本为例子:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSIntegerten_total =5;
NSIntegertotal =powl(10, ten_total);
NSMutableArray *array = [NSMutableArray array];
for(inti =0; i < total; i++) {
[arrayaddObject:[NSStringstringWithFormat:@"%@",@(i)]];
}
[self forRoopOne:array];
[self forRoopInTwo:array];
[self forRoopThree:array];
[self forRoopFour:array];
[self forRoopFive:array];
});
常见的五种遍历方法(也可分为四种while算一种),如下:
- (void)forRoopOne:(NSArray *)params {
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
for(inti =0; i < params.count; i++) {
NSString *item = params[i];
NSLog(@"当前的数据为%@",item);
}
CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent();
NSLog(@" 1: 普通for循环 time cost: %0.3f", endTime - startTime);
}
- (void)forRoopInTwo:(NSArray *)params {
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
for(NSString *iteminparams) {
NSLog(@"当前的数据为%@",item);
}
CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent();
NSLog(@" 2: for-in循环 time cost: %0.3f", endTime - startTime);
}
- (void)forRoopThree:(NSArray *)params {
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
NSIntegerindex =0;
while(index < params.count) {
NSString *item = params[index];
NSLog(@"当前的数据为%@",item);
index ++;
}
CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent();
NSLog(@" 3: while 循环 time cost: %0.3f", endTime - startTime);
}
- (void)forRoopFour:(NSArray*)params {
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
[paramsenumerateObjectsUsingBlock:^(id _Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {
NSString*item = obj;
NSLog(@"当前的数据为%@",item);
}];
CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent();
NSLog(@" 4: enumerateObjectsUsingBlock 循环 time cost: %0.3f", endTime - startTime);
}
- (void)forRoopFive:(NSArray*)params {
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
NSEnumerator*enumerator = [paramsobjectEnumerator];
while(enumerator.nextObject) {
NSLog(@"当前的数据为%@",@(11111));
}
CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent();
NSLog(@" 5: NSEnumerator 循环 time cost: %0.3f", endTime - startTime);
}
1. 数据设置为10条时,执行时间 如下:
总数10 1: 普通for循环 time cost: 0.001
总数10 2: for-in循环 time cost: 0.004
总数10 3: while 循环 time cost: 0.001
总数10 4: enumerateObjectsUsingBlock 循环 time cost: 0.001
总数10 5: NSEnumerator 循环 time cost: 0.000
2. 数据设置为100条时,执行时间如下:
总数100 1: 普通for循环 time cost: 0.010
总数100 2: for-in循环 time cost: 0.005
总数100 3: while 循环 time cost: 0.005
总数100 4: enumerateObjectsUsingBlock 循环 time cost: 0.005
总数100 5: NSEnumerator 循环 time cost: 0.003
3.数据设置为1000条时,执行时间如下:
总数1000 1: 普通for循环 time cost: 0.113
总数1000 2: for-in循环 time cost: 0.107
总数1000 3: while 循环 time cost: 0.104
总数1000 4: enumerateObjectsUsingBlock 循环 time cost: 0.072
总数1000 5: NSEnumerator 循环 time cost: 0.061
4.数据设置为10000条时,执行时间如下:
总数10000 1: 普通for循环 time cost: 1.151
总数10000 2: for-in循环 time cost: 1.505
总数10000 3: while 循环 time cost: 1.872
总数10000 4: enumerateObjectsUsingBlock 循环 time cost: 0.793
总数10000 5: NSEnumerator 循环 time cost: 0.410
5.数据设置为100000条时,执行时间如下:
总数100000 1: 普通for循环 time cost: 7.610
总数100000 2: for-in循环 time cost: 7.473
总数100000 3: while 循环 time cost: 7.521
总数100000 4: enumerateObjectsUsingBlock 循环 time cost: 7.360
总数100000 5: NSEnumerator 循环 time cost: 3.711
综上所述,对比输出结果,存在以下结论:
1. 数据量小时,几种循环方案可随意选择,按照习惯来即可;
2. 数据量大时,for-in可满足日常的使用,或用enumerateObjectsUsingBlock;
3. NSEnumerator用法效率最高;