test1

简单遍历
结论:
当数组数据量很小 时候 for loop 和 for in 效率不相上下,随着数据量增长for in 快速枚举的优势 明显 如果需要知道 索引可用 enumrateBlock

test2

根据value 查找对应index   例如 查找9999999对应索引
结论: 数据量小 for in 最好 enumerateBlock稍弱 但可读性更强 enumerateObjectsWithOptions最弱
数据量大 enumerateObjectsWithOptions最好 其次 enumerateBlock 最差 for in
NSMutableArray *test = [NSMutableArray array];

test3

for in  enumerateObjectsWithOptions:遍历字典
结论 遍历字典 用 enumerateKeysAndObjectsUsingBlock 速度快 代码可读性强

注意要点:

NSEnumerationConcurrent

枚举过程中,(那么底层通过GCD来处理并发执行事宜,具体实现可能会用到dispatch group。也就是说,这个会用多线程来并发实现)各个Block是同时开始执行的。这样枚举的完成顺序是不确定的。

也就是说由于并发处理, 没法同步获取结果(适合 针对里面元素的处理,不能用作 "遍历"加和)

NSEnumerationReverse

以反序方式枚举。

- (void)test1
{//简单遍历
//结论:
//当数组数据量很小 时候 for loop 和 for in 效率不相上下,随着数据量增长for in 快速枚举的优势 明显 如果需要知道 索引可用 enumrateBlock
NSMutableArray *test = [NSMutableArray array];
for (int i = 0; i < 100000; i ++) {
[test addObject:@(i)];
} //for loop
__block int sum = 0;
double date_s = CFAbsoluteTimeGetCurrent();
for (int i = 0; i < test.count; i ++) {
sum += [test[i] integerValue];
}
double date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@"Sum : %d ForLoop Time: %f ms",sum,date_current * 1000); //for in
sum = 0;
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 * 1000); // enumberateObjectes
sum = 0;
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 * 1000); sum = 0;
date_s = CFAbsoluteTimeGetCurrent();
[test enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
sum += [obj integerValue];
}];
date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@"Sum : %d enumrateBlock Time: %f ms",sum,date_current * 1000); // 结果:
// 2016-02-19 11:57:40.561 TableViewDemo[9988:1598542] Sum : 1783293664 ForLoop Time: 41.271031 ms
// 2016-02-19 11:57:40.578 TableViewDemo[9988:1598542] Sum : 1783293664 For-in Time: 17.231047 ms
// 2016-02-19 11:57:40.630 TableViewDemo[9988:1598542] Sum : 1783293664 enumrateBlock Time: 51.365972 ms
} - (void)test2
{//根据value 查找对应index 例如 查找9999999对应索引
//结论: 数据量小 for in 最好 enumerateBlock稍弱 但可读性更强 enumerateObjectsWithOptions最弱
// 数据量大 enumerateObjectsWithOptions最好 其次 enumerateBlock 最差 for in
NSMutableArray *test = [NSMutableArray array];
for (int i = 0; i < 10; i ++) {
[test addObject:@(i + 10)];
} //For-in
__block NSInteger index = 0;
double date_s = CFAbsoluteTimeGetCurrent();
for (NSNumber *num in test) {
if ([num integerValue] == 9999999) {
index = [test indexOfObject:num];
break;
}
}
double date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@"index : %ld For-in Time: %f ms",(long)index,date_current * 1000); //enumerateObjectsUsingBlock
index = 0;
date_s = CFAbsoluteTimeGetCurrent();
[test enumerateObjectsUsingBlock:^(id num, NSUInteger idx, BOOL *stop) {
if ([num integerValue] == 9999999) {
index = idx;
*stop = YES;
}
}];
date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@"index : %ld enumerateBlock Time: %f ms",(long)index,date_current * 1000); //enumerateObjectsWithOptions
index = 0;
date_s = CFAbsoluteTimeGetCurrent();
[test enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id num, NSUInteger idx, BOOL *stop) {
if ([num integerValue] == 9999999) {
index = idx;
*stop = YES;
}
}];
date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@"index : %ld enumerateObjectsWithOptions Time: %f ms",(long)index,date_current * 1000); // 2016-02-19 16:00:54.799 TableViewDemo[11084:1707298] index : 9999989 For-in Time: 754.406035 ms
// 2016-02-19 16:00:55.384 TableViewDemo[11084:1707298] index : 9999989 enumerateBlock Time: 585.359991 ms
// 2016-02-19 16:00:55.806 TableViewDemo[11084:1707298] index : 9999989 enumerateObjectsWithOptions Time: 420.368969 ms
} - (void)test3
{//for in enumerateObjectsWithOptions:遍历字典
//结论 遍历字典 用 enumerateKeysAndObjectsUsingBlock 速度快 代码可读性强
NSDictionary *testDictionary = @{
@"Auther" : @"南望青天",
@"Game" : @"Dota",
@"App" : @"麦刀塔",
@"Market" : @"AppStore"
}; //For - in
NSMutableArray *forInArry = [NSMutableArray array];
double date_s = CFAbsoluteTimeGetCurrent();
NSArray *keys = [testDictionary allKeys];
for (NSString *key in keys) {
NSString *Value = testDictionary[key];
[forInArry addObject:Value];
}
double date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@"index : %ld For-in Time: %f ms",(long)index,date_current * 1000); //enumerateKeysAndObjectsUsingBlock
date_s = CFAbsoluteTimeGetCurrent();
NSMutableArray *enumArry = [NSMutableArray array];
[testDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[enumArry addObject:obj];
}];
date_current = CFAbsoluteTimeGetCurrent() - date_s;
NSLog(@"index : %ld Dic Time: %f ms",(long)index,date_current * 1000); NSLog(@"ForInArr: %@",forInArry);
NSLog(@"enumArry: %@",enumArry);
// 2016-02-19 16:26:35.307 TableViewDemo[11268:1721020] index : 4516026384 For-in Time: 0.015974 ms
// 2016-02-19 16:26:35.308 TableViewDemo[11268:1721020] index : 4516026384 Dic enumerateKeysAndObjectsUsingBlock Time: 0.006974 ms
}

参考博客 http://www.jianshu.com/p/ef3f1731a353

iOS 快速遍历 效率分析 for loop for in enumerateBlock 适用条件的更多相关文章

  1. IOS各种集合遍历效率对比

    前言: 对于ios项目开发中总会遇见各种集合遍历,出于对各种遍历效率的好奇心,所以准备写个测试程序测试一下 首先:先声明一个NSMutableArray,测试数据量分别是1000条,10000条,10 ...

  2. mySql执行效率分析

    1.关于SQL查询效率,100w数据,查询只要1秒,与您分享: 机器情况p4: 2.4内存: 1 Gos: windows 2003数据库: ms sql server 2000目的: 查询性能测试, ...

  3. JDK8 Stream 数据流效率分析

    JDK8 Stream 数据流效率分析 Stream 是Java SE 8类库中新增的关键抽象,它被定义于 java.util.stream (这个包里有若干流类型: Stream<T> ...

  4. ArrayList哪种遍历效率最好,你真的弄明白了吗?

    ArrayList简介 声明:以下内容都是基于jdk1.8的 ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了 ...

  5. OpenCV快速遍历矩阵元素方法

    OpenCV中Mat矩阵data数据的存储方式和二维数组不一致,二维数组按照行优先的顺序依次存储,而Mat中还有一个标示行步进的变量Step.使用Mat.ptr<DataTyte>(row ...

  6. in和exists的区别与SQL执行效率分析

    可总结为:当子查询表比主查询表大时,用Exists:当子查询表比主查询表小时,用in SQL中in可以分为三类: 1.形如select * from t1 where f1 in ('a','b'), ...

  7. mssql分页原理及效率分析

    下面是常用的分页,及其分页效率分析. 1.分页方案一:(利用Not In和SELECT TOP分页) 语句形式: SELECT TOP 10 * FROM TestTable WHERE (ID NO ...

  8. 【HELLO WAKA】WAKA iOS客户端 之一 APP分析篇

    由于后续篇幅比较大,所以调整了内容结构. 全系列 [HELLO WAKA]WAKA iOS客户端 之一 APP分析篇 [HELLO WAKA]WAKA iOS客户端 之二 架构设计与实现篇 [HELL ...

  9. 【转】NO.3、python+appium+ios,遍历真机元素,得到webview

    pyhton+appium+iOS,遍历真机webview.是遍历真机的webview,遍历模拟器的webview请另寻方法. 1.mac上安装ios_webkit_debug_proxy 命令:br ...

随机推荐

  1. IOS设计模式的六大设计原则之依赖倒置原则(DIP,Dependence Inversion Principle)

    定义 高层模块不应该依赖于低层模块,二者都应该依赖于抽象:抽象不应该依赖细节:细节应该依赖抽象. 定义解读 依赖倒置原则在程序编码中经常运用,其核心思想就是面向接口编程,高层模块不应该依赖低层模块(原 ...

  2. Linux 常用文件管理命令

    Command Description cat filename 查看文件内容.举例:cat pushticketfast.sh   --最好用tail -n 500  cd dirname 改变所在 ...

  3. python 自动化之路 day 19 Django基础[二]

    Django - 路由系统 url.py - 视图函数 views.py - 数据库操作 models.py - 模板引擎渲染 - HttpReponse(字符串) - render(request, ...

  4. Python Theano 一键安装

    Download Anaconda Anaconda is a completely free Python distribution (including for commercial use an ...

  5. Word常用操作笔记

    总忘,在这记一下,以下以WPS为例,WORD大同小异,别看网上那些乱写的,就哥这个稳定好使: 1. 在指定页面及其后开始插入页码 2. 修改页码中的总页数 -> 选中总页数的域 -> SH ...

  6. springmvc 生命周期

    1A)客户端发出http请求,只要请求形式符合web.xml 文件中配置的*.action的话,就由DispatcherServlet 来处理. 1B)DispatcherServlet再将http请 ...

  7. 如何在shell中处理异常(转)

    似乎好像大概有句话是这么说得,好程序与坏程序之间的区别就在于它的鲁棒性,也就是在异常情况下该程序是否还是在可hold住状态,能否不死,不崩溃,或者不做出一些超出预期的事情.那要做好这些,自然而然就要学 ...

  8. mongo 过滤查询条件后分组、排序

    描述:最近业主有这么一个需求,根据集合中 时间段进行过滤,过滤的时间时间段为日期类型字符串,需要根据某一日期进行截取后.进行分组,排序 概述题目:根据createTime时间段做查询,然后以 天进行分 ...

  9. CodeIgniter框架——数据库类(配置+快速入门)

    CodeIgniter用户指南——数据库类 数据库配置 入门:用法举例 连接数据库 查询 生成查询结果 查询辅助函数 Active Record 类 事务 表格元数据 字段元数据 自定义函数调用 查询 ...

  10. outlook撤回已发送邮件

    官方教程参考: https://support.office.com/zh-cn/article/%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E5%90%8E%E6%92 ...