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; ...
随机推荐
- edittext_解释
============ 2 android判断EditText输入的数字.中文还是字母方法 String txt = edInput.getText().toString(); Patter ...
- PHP 开发的 API 多版本管理实践
遇到的情况 本文针对移动互联网客户端需要兼容旧版的情况,强制升级到最新版本的 app 不在讨论之列. 在 bugtags.com 项目中,我们的版本遵循下面规范.1.0.1大功能.小更新.bug 修正 ...
- itextSharp 附pdf文件解析
一.PdfObject: pdf对象 ,有9种,对象是按照对象内涵来分的,如果按照对象的使用规则来说,对象又分为间接对象和直接对象.间接对象是PDF中最常用的对象,如前面对象集合里面的,所有对象都是间 ...
- sql语句,怎么查看一个表中的所有约束
sql语句,怎么查看一个表中的所有约束,比如,一个student表,有唯一,外键,主键,用sql语句怎么查看student表中的所有约束呢? select * from sysobjects wher ...
- day8-多进程和多线程
Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. import threading import time def sayhi(num): #定义每个线程要 ...
- 66. Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- python发邮件
# -*- coding:utf- -*- import smtplib,os,sys,string import mimetypes from email import Encoders from ...
- HtmlAgilityPack下载开启压缩的页面乱码
当一个被采集的网页是开启压缩了的话,如果使用HtmlAgilityPack 的HtmlWeb默认配置去下载,下载回来的HTML代码是乱码,应该进行如下操作 HtmlWeb web = new Html ...
- 自定义属性,资源文件attrs.xml
1.attrs.xml中写:在values文件夹下. <?xml version="1.0" encoding="utf-8"?> <reso ...
- delphi软件启动的顺序解密。
运行顺序 1.主窗体的oncreate -- onshow ---- onActivate ---- onResize --- 然后继续走,这个时候主窗体已经显示出来了,猜想delphi的思路是先让主 ...