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; ...
随机推荐
- 2 、Linux基本命令-ls-pwd-cd-date-hwclock
Linux基本命令: 1.ls-查看目录下的文档 语法: ls 目录 注: .当前目录 ..上级目录 如:ls /etc/ 相关参数: -l 显示详细信息 ls /etc/ -l -a 显示隐藏的 ...
- DirectBuffer
1.如何分配,分配是哪里的内存 ByteBuffer.allocateDirect()来分配(ByteBuffer.allocate()分配堆内内存),分配的是非Heap(堆外)的内存,不排除操作系统 ...
- PNG与iOS优化选项
从App Store下载到的每一枚App最初都是一只IPA文件(其实是zip格式,内含特定规则的文件夹组织方式).但当作zip解开之后会发现里面很多的PNG文件看不了,这是因为在这些PNG图像都已被i ...
- php使用递归计算目录大小
本文章向大家介绍php如何计算某个目录的大小(多少kb,多少兆m),主要使用filesize函数配合递归函数的方法来实现,需要的朋友可以参考一下本文章的源代码.php使用递归计算目录大小,主要使用fi ...
- fw: openstack
OpenStack既是一个社区,也是一个项目和一个开源软件,它提供了一个部署云的操作平台或工具集.其宗旨在于,帮助组织运行为虚拟计算或存储服务的云,为公有云.私有云,也为大云.小云提供可扩展的.灵活的 ...
- MVC4.0中项目发布遇到IE11时session存入URL中,导致记不住密码的问题
///MVC4.0中项目发布遇到IE11时session存入URL中,导致记不住密码的问题,在webconfig中配置<system.web><authentication mode ...
- JS,Jquery获取各种屏幕的宽度和高度
Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...
- webForm中dropDownList的一些用法
DropDownList 控件用于创建下拉列表. DropDownList 控件中的每个可选项都是由 ListItem 元素定义的! 该控件支持数据绑定! DropDownList1.DataSour ...
- ORA-01704: string literal too long
update mkt_page_links set longdescription = ' {some html text > 4000 char} ' where menuidno = 310 ...
- Web开发新时代
Web1.0===主流技术:HTML+CSS Web2.0===主流技术:Ajax(Javascript/DOM/异步数据请求) Web3.0===主流技术:HTML5+CSS3 HTML5亮点:Ca ...