enumerateObjectsUsingBlock 、for 、for(... in ...) 的区别 & 性能测试
for VS for(... in ...)
- for 的应用范围广基本可以NSArray、NSArray以及C语言的数组等,而for(... in ...)仅限于NSArray、NSArray等
- for(... in ...) 更简洁、效率更高
测试代码:
10^7 的数组,时间单位 秒,精确度 毫秒
NSMutableArray *test = [NSMutableArray array];
for (int i= ; i < ; i++) {
[test addObject:@(i)];
}
int sum = ; double date_s = CFAbsoluteTimeGetCurrent();
for (int i = ;i < test.count; i++) {
sum += ;
}
double date_e = CFAbsoluteTimeGetCurrent();
NSLog(@"ForLoop Time: %f", date_e - date_s); date_s = CFAbsoluteTimeGetCurrent();
for (id obj in test) {
sum += ;
}
date_e = CFAbsoluteTimeGetCurrent();
NSLog(@"Enumeration Time: %f", date_e - date_s);
测试结果:

考虑到实际情况,ForLoop 的操作较多些。
测试代码:
硬件:i5 Cpu, 10G 内存,Mac OS X 10.9.4
数据量:10^7 的数组,
时间:单位 秒,精确度 毫秒
NSMutableArray *test = [NSMutableArray array];
for (int i= ; i < ; i++) {
[test addObject:@(i)];
}
int sum = ; double date_s = CFAbsoluteTimeGetCurrent();
for (int i = ;i < test.count; i++) {
int key = [test[i] intValue];
sum += key;
sum -= key;
}
double date_e = CFAbsoluteTimeGetCurrent();
NSLog(@"ForLoop Time: %f", date_e - date_s); date_s = CFAbsoluteTimeGetCurrent();
for (id obj in test) {
int key = [obj intValue];
sum += key;
sum -= key;
}
date_e = CFAbsoluteTimeGetCurrent();
NSLog(@"Enumeration Time: %f", date_e - date_s);
测试结果:

enumerateObjectsUsingBlock VS for(... in ...)
for(... in ...)用起来非常方便、简洁,同时 enumerateObjectsUsingBlock: 也有很多新特性:
通常enumerateObjectsUsingBlock:和 (for(... in ...)在效率上基本一致,有时会快些。主要是因为它们都是基于NSFastEnumeration实现的. 快速迭代在处理的过程中需要多一次转换,当然也会消耗掉一些时间. 基于Block的迭代可以达到本机存储一样快的遍历集合. 对于字典同样适用,而数组的迭代却不行。注意"enumerateObjectsUsingBlock" 修改局部变量时, 你需要声明局部变量为
__block 类型.enumerateObjectsWithOptions:usingBlock:支持并发迭代或反向迭代,并发迭代时效率也非常高.对于字典而言,
enumerateObjectsWithOptions:usingBlock也是唯一的方式可以并发实现恢复Key-Value值.
就个人而言, 我偏向于使用 enumerateObjectsUsingBlock: 当然最后还是要根据实际情况上下文决定用什么
测试代码:
硬件:i5 Cpu, 10G 内存,Mac OS X 10.9.4
数据量:10^4 的数组,执行一次NSLog输出
时间:单位 秒,精确度 毫秒
NSMutableArray *test = [NSMutableArray array];
for (int i= ; i < ; i++) {
[test addObject:@(i)];
} double date_s = CFAbsoluteTimeGetCurrent();
for (id obj in test) {
NSLog(@"%@",obj);
}
double date_e = CFAbsoluteTimeGetCurrent();
NSLog(@"ForLoop Time: %f", date_e - date_s); date_s = CFAbsoluteTimeGetCurrent();
// [test enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// NSLog(@"%@",obj);
// }];
[test enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@",obj);;
}];
date_e = CFAbsoluteTimeGetCurrent();
NSLog(@"Enumeration Time: %f", date_e - date_s);
测试结果:
// ForLoop Time: 14.951485
// Default Enumeration Time: 14.702673
// Reverse Enumeration Time: 14.948526
// Concurrent Enumeration Time: 10.056317
参考:
http://stackoverflow.com/questions/4486622/when-to-use-enumerateobjectsusingblock-vs-for
enumerateObjectsUsingBlock 、for 、for(... in ...) 的区别 & 性能测试的更多相关文章
- HttpURLConnection与 HttpClient 区别/性能测试对比
HttpClient是个开源框架,封装了访问http的请求头,参数,内容体,响应等等, HttpURLConnection是java的标准类,什么都没封装,用起来太原始,不方便 HttpClient实 ...
- Swift-(OC中的enumerateObjectsUsingBlock跟Swift的enumerate区别)
OC中使用: NSArray * lists = [NSArray array]; [lists enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUI ...
- loadrunner总结
loadrunner总结 1.性能测试包含了哪些测试(至少举出3种) 负载测试,压力测试,疲劳强度测试,大数据量测试,并发测试. 2.负载测试和压力测试的区别 性能测试: 是通过自动化的测试工具模拟多 ...
- MySQL数据库引擎介绍、区别、创建和性能测试的深入分析
本篇文章是对MySQL数据库引擎介绍.区别.创建和性能测试进行了详细的分析介绍,需要的朋友参考下 数据库引擎介绍 MySQL数据库引擎取决于MySQL在安装的时候是如何被编译的.要添加一个新的引擎 ...
- [转]MySQL数据库引擎介绍、区别、创建和性能测试的深入分析
本篇文章是对MySQL数据库引擎介绍.区别.创建和性能测试进行了详细的分析介绍,需要的朋友参考下 数据库引擎介绍 MySQL数据库引擎取决于MySQL在安装的时候是如何被编译的.要添加一个新的引擎 ...
- 2015第22周一Web性能测试工具及IE扩展区别
在高性能web测试工具推荐http://www.jb51.net/article/23034.htm中发现了dynaTrace 感觉很不错,不但可以检测资源加载瀑布图,而且还能监控页面呈现时间,CPU ...
- ArrayList和LinkedList区别及性能测试
ArrayList和LinkedList是Java Lis接口的2个实现.它们的区别如下表所示: 底层结构 强项 弱项 ArrayList 数组 随机访问get和set 插入删除 LinkedList ...
- arraylist与linkedlist的区别与性能测试
/** *arraylist和linkedlist的适用场合. **/ import java.util.List; import java.util.ArrayList; import java.u ...
- 性能测试工具LoadRunner中进程运行和线程运行区别
loadrunner controller将使用驱动程序mmdrv运行Vuser.用户可以在controller的run-time setting中选择Vuser的运行方式, 是多进程方式or多线程方 ...
随机推荐
- Visio2010如何安装
双击setup. 点击我接受此协议的条款,然后点击继续. 这里选择自定义,很重要哦,不要选择立即安装,不然,一会装完后,你会找不到快捷方式的. 文件位置这里选择好存放路径,一会我们要去这里 ...
- Python学习笔记6-字典Dict
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. >>> person ...
- oracle 在sql中拼接时间
<isNotEmpty prepend="and" property="startDate"> to_date(rs.count_date, 'YY ...
- Intent讲解
什么是Intent? Intent是一个消息传递对象,可以使用它来启动其它应用组件.Intent使组件之间通信更加便利,主要用于以下三点: 启动Activity: 可以将intent作为参数调用Con ...
- JSP内置对象——request 及其响应get和post请求的实例
request对象客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例.request对象具有请求域,即完成客户端的 ...
- ndarray 布尔类型矩阵中统计Ture 的次数
对象:NumPy数组或矩阵,eg. data的元素为True和False numpy.sum(data) #统计data中True的个数numpy.count_nonzero(data) #统计dat ...
- Linux上查看和修改字符集
author :headsen chen date: 2018-05-14 16:20:30 一·查看字符集 字符集在系统中体现形式是一个环境变量,看当前终端使用字符集的有以下几种方式: 1: 1 ...
- 《从零开始学Swift》学习笔记(Day 68)——Cocoa Touch设计模式及应用之响应者链与触摸事件
原创文章,欢迎转载.转载请注明:关东升的博客 应用与用户进行交互,依赖于各种各样的事件.事件响应者对象是可以响应事件并对其进行处理的对象,响应者链是由一系列链接在一起的响应者组成的.响应者链在事件处理 ...
- 160518、java中使用百度地图(超级简单)
第一步:导入如下内容,红色的部分需要申请(个人通过手机号就可以申请) <script type="text/javascript" src="http://api. ...
- Linux 常用资源
kernel:ftp://kernel.orgcnkernel:http://www.cnkernel.orgoldlinux:http://www.oldlinux.orgminix3:http:/ ...