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多线程方 ...
随机推荐
- windows环境通过cmd命令到ftp上下载文件到linux服务器
转自:http://jingyan.baidu.com/article/6525d4b1300912ac7d2e941b.html
- Codeforces Round #359 (Div. 2) C. Robbers' watch 搜索
题目链接:http://codeforces.com/contest/686/problem/C题目大意:给你两个十进制的数n和m,选一个范围在[0,n)的整数a,选一个范围在[0,m)的整数b,要求 ...
- U盘插入拔出提示
Unit Unit1; Interface Uses Windows, Messages, SysUtils, Variants, classes, Graphics, Controls, Forms ...
- 【黑金原创教程】【TimeQuest】【第三章】TimeQuest 扫盲文
声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/al ...
- 【BZOJ3033】太鼓达人 暴力+欧拉回路
[BZOJ3033]太鼓达人 Description 七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行.这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是刚刚被精英队伍成员 ...
- LeetCode-Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- eclipse中设置在编译运行项目之前自动保存修改的文件
Window -> Preferences -> General -> Workspace -> “Save automatically before build” Windo ...
- 用linux c求最大公约数
我写了两中函数,一个是辗转相除法一个是更相减损法,主要代码如下: /*辗转相除法*/int gcd(int a, int b) { ) { return b; } else { return gcd( ...
- 004-hadoop家族概述
hadoop家族 名称 简介 Hadoop 分布式基础架构 Hadoop的框架最核心的设计就是:HDFS和MapReduce.HDFS为海量的数据提供了存储,则MapReduce为海量的数据提供了 ...
- Oracle中to_number()函数的用法
to_number()函数是oracle中常用的类型转换函数之一,是将一些处理过的按一定格式编排过的字符串变回数值型的格式. 1.to_number()函数可以将char或varchar2类型的str ...