如果我們要遍歷一個(gè)數(shù)組, 上過編程課程的童鞋都會(huì)想到For語(yǔ)句去循環(huán).
Objective C 提供一個(gè)Block的遍歷方法, 那么用它還是用For好呢弓千?下面我們?nèi)?shí)踐一下:
遍歷一個(gè)數(shù)組看誰(shuí)快
參賽選手 ForLoop, For - in, enumerateObjectsUsingBlock這個(gè)三個(gè)方法:
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);
最后輸出如下:
打印輸出: For-in方法最快速
結(jié)論:
當(dāng)只是遍歷一個(gè)數(shù)組的時(shí)候使用For-in會(huì)比較快速, 推薦使用For-in遍歷數(shù)組.
通過Value查找Index看誰(shuí)快
假如現(xiàn)在我們要查找一個(gè)Value, 這個(gè)Value 值是100001, 找出它的index (數(shù)組的序列號(hào)).
那么現(xiàn)在我們來(lái)比較一下
參賽選手 For - in, enumerateObjectsUsingBlock, enumerateObjectsWithOptions 這個(gè)三個(gè)方法: (ForLoop已經(jīng)不再繼續(xù)討論了)
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);
最后輸出如下圖:
打印輸出: enumerateObjectsWithOptions方法最快速
結(jié)論:
通過Value查詢index的時(shí)候, 面對(duì)大量的數(shù)組推薦使用 enumerateObjectsWithOptions的并行方法.
For-in和enumerateObjectsWithOptions方法這里我比較喜歡第二種寫法簡(jiǎn)潔直觀.
現(xiàn)在咱們要遍歷字典,這里我們比較一下使用 For-in 和 enumerateKeysAndObjectsUsingBlock 這個(gè)兩個(gè)方法:
NSDictionary *testDictionary = @{
@"Auther" : @"Jane",
@"Game" : @"Dota",
@"App" : @"JJSHome",
@"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);
最后輸出如下圖:
打印輸出: enumerateKeysAndObjectsUsingBlock勝出
結(jié)論:
當(dāng)我們想遍歷字典類型的時(shí)候, 推薦使用enumerateKeysAndObjectsUsingBlock
不僅僅是因?yàn)樗俣瓤? 更是因?yàn)榇a更優(yōu)雅和直觀.