NSArray其中的方法--遍历,
1. ForLoop, For - in, enumerateObjects这个三个方法的区别:
遍历一个数组用For-in最快.
通过Value查询index的时候, 面对大量的数组推荐使用 enumerateObjectsWithOptions的并行方法.
遍历字典类型的时候, enumerateKeysAndObjectsUsingBlock效率最高
1.1遍历数组
NSMutableArray *test = [NSMutableArray array];
for (int i = ; i < ; i ++) {
[test addObject:@(i)];
}
//ForLoop方法
__block int sum = ;
double date_s = CFAbsoluteTimeGetCurrent();
for (int i = ; i < test.count; i ++) {
sum += [test[i] integerValue];
}
double date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@"Sum : %d ForLoop Time: %f ms",sum,date_current * );
//For - in方法
sum = ;
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 * );
//enumerateObjectsUsingBlock方法
sum = ;
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 * );
打印如下:

总结:遍历一个数组用For-in最快.
1.2-通过Value查找Index看谁快
实验:For - in, enumerateObjectsUsingBlock, enumerateObjectsWithOptions 这个三个方法: (ForLoop已经不再继续讨论了)
NSMutableArray *test = [NSMutableArray array];
for (int i = ; i < ; i ++) {
[test addObject:@(i + )];
} //For-in
__block NSInteger index = ;
double date_s = CFAbsoluteTimeGetCurrent();
for (NSNumber *num in test) {
if ([num integerValue] == ) {
index = [test indexOfObject:num];
break;
}
}
double date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@"index : %ld For-in Time: %f ms",(long)index,date_current * ); //enumerateObjectsUsingBlock
index = ;
date_s = CFAbsoluteTimeGetCurrent();
[test enumerateObjectsUsingBlock:^(id num, NSUInteger idx, BOOL *stop) {
if ([num integerValue] == ) {
index = idx;
*stop = YES;
}
}];
date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@"index : %ld enumerateBlock Time: %f ms",(long)index,date_current * ); //enumerateObjectsWithOptions
index = ;
date_s = CFAbsoluteTimeGetCurrent();
[test enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id num, NSUInteger idx, BOOL *stop) {
if ([num integerValue] == ) {
index = idx;
*stop = YES;
}
}];
date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@"index : %ld enumerateObjectsWithOptions Time: %f ms",(long)index,date_current * );
打印:

结论:通过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 * );
//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 * );
//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 * );
打印:

结论:遍历字典类型的时候, enumerateKeysAndObjectsUsingBlock效率最高
NSArray其中的方法--遍历,的更多相关文章
- javacpp-opencv图像处理3:使用opencv原生方法遍历摄像头设备及调用(增加实时帧率计算方法)
javaCV图像处理系列: javaCV图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体.位置.大小.粗度.翻转.平滑等操作 javaCV图像处理之2:实时视频添加图片水 ...
- js,jquery中.each()方法遍历如何终止循环
用.each()方法遍历节点的时候,用“return false”只能终止当前循环并跳入下一次循环,并不能终止所有循环.代码如下: $(".days").each(function ...
- Swift中实现Array数组和NSArray数组的相互转换与遍历
Array是Swift中的数组数据类型.而NSArray是OC中的数组数据类型.两者有差别有联系.在Swift中有时候难免会使用到OC中的一些东西.今天我们就来Swift中使用NSArray和Arra ...
- PHP使用glob方法遍历文件夹下所有文件
PHP使用glob方法遍历文件夹下所有文件 遍历文件夹下所有文件,一般可以使用opendir 与 readdir 方法来遍历.<pre><?php$path = dirname(__ ...
- Python中使用item()方法遍历字典的例子
Python中使用item()方法遍历字典的例子 这篇文章主要介绍了Python中使用item()方法遍历字典的例子,for...in这种是Python中最常用的遍历字典的方法了,需要的朋友可以参考下 ...
- [OC Foundation框架 - 7] NSArray的创建与遍历
NSArray是不可变的,不能先创建再添加元素 NSArray可以放入任何OC对象,但不能放入基本数据类型.结构体.枚举等非OC对象 不能存储nil A.常用方法1 创建 返回用量 是否含有某元素 ...
- NSArray 的创建和遍历
数组 用来存贮对象的有序列表,它是不可变的 不能存数C语言的基本数据类型 只支持OC对象 #pragma mark Create a array //Initialize NSArray void a ...
- NSArray,NSSet,NSDictionary的遍历,基本使用集锦
NSArray *array = [NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangwu&quo ...
- STL中用erase()方法遍历删除元素 .xml
pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...
随机推荐
- (转)原始图像数据和PDF中的图像数据
比较原始图像数据和PDF中的图像数据,结果见表1.1.表1.1中各种“解码器”的解释见本文后续的“PDF支持的图像格式”部分,“PDF中的图像数据”各栏中的数据来自开源的PdfView.如果您有兴趣查 ...
- el 中requestScope和param
${scope.attribute},其中scope指pageSocpe.requestScope.sessionScope.applicationScope,attribute指的就是你在某个sco ...
- 在IIS8添加WCF服务支持
最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少对WCF ...
- 使用nginx部署Yii 2.0\yii-basic-app-2.0.5
nginx.conf #user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log noti ...
- 9. Sort List && Insertion Sort List (链表排序总结)
Sort List Sort a linked list in O(n log n) time using constant space complexity. H ...
- c/c++ 对象内存布局
一.对象内存查看工具 VS 编译器 CL 的一个编译选项可以查看 C++ 类的内存布局,非常有用.使用如下,从开始程序菜单找到 Visual Stdio 2012. 选择 VS 的命令行工具,按如下格 ...
- 用移动智能设备访问Ossim系统
用移动智能设备访问Ossim系统 下面我们使用iPad,iPhone访问ossim系统的效果. 高清视频:http://www.tudou.com/programs/view/TikMZ1z1ELw ...
- PCI Express(一)- Connector
在FPGA4FUN上看到一篇介绍PCI-E的帖子,简单易懂,适合入门,特地搬过来 原文地址:http://www.fpga4fun.com/PCI-Express.html 前言: As PCI Ex ...
- java学习第三天
关于上次的数据转换,如果定义的是 short 是,那么s=s+1,这样是错误的,会损失精度,但如果是s+=1.确实正确的,因为它包含了一个强制转化在里面.相当于 s=(int)(s+1); 然后特地 ...
- JS中的for/in语句和arguments参数
在js应用中,有时候我们会遇到希望定义一个函数可以被不同情况调用,比如参数个数不固定的情况. 可以使用for/in语句和arguments参数解决. 举个简单例子:定义一个计算总和的函数,需要计算的数 ...