1. ForLoopFor - inenumerateObjects这个三个方法的区别:

遍历一个数组用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 - inenumerateObjectsUsingBlockenumerateObjectsWithOptions 这个三个方法: (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其中的方法--遍历,的更多相关文章

  1. javacpp-opencv图像处理3:使用opencv原生方法遍历摄像头设备及调用(增加实时帧率计算方法)

    javaCV图像处理系列: javaCV图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体.位置.大小.粗度.翻转.平滑等操作 javaCV图像处理之2:实时视频添加图片水 ...

  2. js,jquery中.each()方法遍历如何终止循环

    用.each()方法遍历节点的时候,用“return false”只能终止当前循环并跳入下一次循环,并不能终止所有循环.代码如下: $(".days").each(function ...

  3. Swift中实现Array数组和NSArray数组的相互转换与遍历

    Array是Swift中的数组数据类型.而NSArray是OC中的数组数据类型.两者有差别有联系.在Swift中有时候难免会使用到OC中的一些东西.今天我们就来Swift中使用NSArray和Arra ...

  4. PHP使用glob方法遍历文件夹下所有文件

    PHP使用glob方法遍历文件夹下所有文件 遍历文件夹下所有文件,一般可以使用opendir 与 readdir 方法来遍历.<pre><?php$path = dirname(__ ...

  5. Python中使用item()方法遍历字典的例子

    Python中使用item()方法遍历字典的例子 这篇文章主要介绍了Python中使用item()方法遍历字典的例子,for...in这种是Python中最常用的遍历字典的方法了,需要的朋友可以参考下 ...

  6. [OC Foundation框架 - 7] NSArray的创建与遍历

    NSArray是不可变的,不能先创建再添加元素 NSArray可以放入任何OC对象,但不能放入基本数据类型.结构体.枚举等非OC对象 不能存储nil   A.常用方法1 创建 返回用量 是否含有某元素 ...

  7. NSArray 的创建和遍历

    数组 用来存贮对象的有序列表,它是不可变的 不能存数C语言的基本数据类型 只支持OC对象 #pragma mark Create a array //Initialize NSArray void a ...

  8. NSArray,NSSet,NSDictionary的遍历,基本使用集锦

    NSArray *array = [NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangwu&quo ...

  9. STL中用erase()方法遍历删除元素 .xml

    pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...

随机推荐

  1. 使用 Jmeter 做 Web 接口测试

    接口测试概述 定义 API testing is a type of software testing that involves testing application programming in ...

  2. 开放地址法实现HashTable

    前注:本文不是讲解Java类库的Hashtable实现原理,而是根据计算机哈希表原理自己实现的一个Hashtable. HashTable内部是用数组存放一个(Key-Value pair)键值对的引 ...

  3. [译] Python 3.5 协程究竟是个啥

    转自:http://blog.rainy.im/2016/03/10/how-the-heck-does-async-await-work-in-python-3-5/ [译] Python 3.5 ...

  4. Zerojudge解题经验交流

    题号:a001: 哈囉 背景知识:输出语句,while not eof 题号:a002: 簡易加法 背景知识:输出语句,while not eof,加法运算 题号:a003: 兩光法師占卜術 背景知识 ...

  5. No.3__C#

    起步的迷思 周二:今天起来,天上下着小雨,气温降低了许多.从上周的二十多度又回到了七八度的样子.多穿了一件衣服,顶着寒风就出门了,确实是有点冷.到了公司,已经八点四十几了.有些小疲倦,头晕晕沉沉的,不 ...

  6. JAVA设计模式之解释器模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述解释器(Interpreter)模式的: 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个 ...

  7. js获取节点的DOM操作

    一直感觉DOM节点什么的乱七八糟的不能理解,可能博客也没办法写清楚,只能把知道的一些信息大致写下来慢慢再补充了. HTML DOM类型 Node类型 Javascript中的所有节点类型都继承自Nod ...

  8. MongoEngine简易教程(转)

    原文:http://www.xefan.com/archives/84063.html Mongoengine教程(1)——概述 Mongoengine教程(2)——文档模式 Mongoengine教 ...

  9. json 拼接多个对象

    var json = {}; var json1 = {a:1,b:1}; var json2 = {c:1,d:1}; json = eval('('+(JSON.stringify(json1)+ ...

  10. mysql主从复制的一些东西的整理

    最近给新上线的项目进行主从结构的搭建,因此整理些有用的东西出来,供作记录: 一.mysql主从复制的一般配置步骤: 1.准备两台数据库环境,或者单台多实例的环境,能够正常的启动和登陆. 2.配置my. ...