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; ...
随机推荐
- 详细安装ss的过程(vultr)
#更新程序yum update -y #安装setuptoolsyum install -y python-setuptools #安装pipeasy_install pip #安装shadowsoc ...
- jquery 文本域光标操作(选、添、删、取)
一.JQuery扩展 ; (function ($) { /* * 文本域光标操作(选.添.删.取)的jQuery扩展 http://www.cnblogs.com/phpyangbo/p/55286 ...
- 排列 && 组合
最近编程经常遇到需要 排列&&组合(求子集) 的问题:遂整理一下. 1. 数字的排列与组合(递归):O(n!),O(nC(n,k)) * O(n) #include <stdio ...
- Django学习笔记(一)
1.$python manage.py runserver 0.0.0.0:8000 开放所有IP $python manage.py runserver 8000 制定开放的端口 2.报错信息: ...
- Server 2003序列号
windows2003 64位注册码 Windows 2003 R2 64bit Enterprise VOL Edition 企业版 MR78C-GF2CY-KC864-DTG74-VMT73 VP ...
- javascript解析引擎(每天有学习一点篇)
======================================================= 有一段时间,经常耳闻web前端的福音,对高性能的V8议论纷纷. 其实对js解析引擎没有深 ...
- 初识selenium
今天尝试了一些selenium,感觉并没有想象中那么难.整理一篇笔记出来. 笔者使用的是Python+selenium.以下内容均是基于Windows系统和Python3.5.2. 首先是下载sele ...
- 基于案例贯通 Spark Streaming 流计算框架的运行源码
本期内容 : Spark Streaming+Spark SQL案例展示 基于案例贯穿Spark Streaming的运行源码 一. 案例代码阐述 : 在线动态计算电商中不同类别中最热门的商品排名,例 ...
- oracle空表导出的问题
之前再做项目的时候下载了一个开源的程序,数据库在移植的时候通过exp/imp导入导出,结果程序在启动时报错,对比过后发现两个数据库表相差了十几个,再排查问题,发现少掉的十几个表全部是空表,查了一下or ...
- PHP指定字段的多维数组排序方法
PHP数组排序可以用array_multisort方法实现,但是如果是多维数组,并且我们要指定数组中的某个字段进行排序,那么这就需要我们自己写方法实现了. function sortArrByFiel ...