for VS for(... in ...)

  1. for 的应用范围广基本能够NSArray、NSArray以及C语言的数组等,而for(... in ...)仅限于NSArray、NSArray等
  2. for(... in ...) 更简洁、效率更高

測试代码:

  10^7 的数组。时间单位 秒,准确度 毫秒

    NSMutableArray *test = [NSMutableArray array];
for (int i= 0; i < 10000000; i++) {
[test addObject:@(i)];
}
int sum = 0; double date_s = CFAbsoluteTimeGetCurrent();
for (int i = 0;i < test.count; i++) {
sum += 1;
}
double date_e = CFAbsoluteTimeGetCurrent();
NSLog(@"ForLoop Time: %f", date_e - date_s); date_s = CFAbsoluteTimeGetCurrent();
for (id obj in test) {
sum += 1;
}
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= 0; i < 10000000; i++) {
[test addObject:@(i)];
}
int sum = 0; double date_s = CFAbsoluteTimeGetCurrent();
for (int i = 0;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= 0; i < 10000; 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 ...) 的差别 &amp; 性能測试的更多相关文章

  1. mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry &#39;xxx&#39; for key &#39;PRIMARY&#39;

    mysql主键设置成auto_increment时,进行并发性能測试出现主键反复Duplicate entry 'xxx' for key 'PRIMARY' 解决方法: 在my.cnf的[mysql ...

  2. Linux 性能測试工具

    Linux 性能測试工具 linux performance 查看系统配置 查看CPU信息 lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64- ...

  3. JNI/NDK开发指南(九)——JNI调用性能測试及优化

    转载请注明出处:http://blog.csdn.net/xyang81/article/details/44279725 在前面几章我们学习到了.在Java中声明一个native方法,然后生成本地接 ...

  4. mongodb3.0 性能測试报告 一

    mongodb3.0 性能測试报告 一 mongodb3.0 性能測试报告 二 mongodb3.0 性能測试报告 三 測试环境: 服务器:X86 pcserver   共6台 cpu:  单颗8核 ...

  5. Window平台搭建Redis分布式缓存集群 (一)server搭建及性能測试

    百度定义:Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对很多其它.包含string(字符串).list(链表).set(集合).zset(sort ...

  6. 系统吞吐量、TPS(QPS)、用户并发量、性能測试概念和公式

    PS:以下是性能測试的主要概念和计算公式,记录下: 一.系统吞度量要素: 一个系统的吞度量(承压能力)与request对CPU的消耗.外部接口.IO等等紧密关联.单个reqeust 对CPU消耗越高, ...

  7. LR杂记 - 性能測试指标及经常使用的监控工具

    监控指标 性能測试通常须要监控的指标包含: 1.serverLinux(包含CPU.Memory.Load.I/O). 2.数据库:1.Mysql 2.Oracle(缓存命中.索引.单条SQL性能.数 ...

  8. PHP归档phar性能測试

    PHP自从5.3后新增PHAR归档,Phar 归档的概念来自 Java™ 技术的 JAR 归档,它同意使用单个文件打包应用程序.这个文件里包括运行应用程序所需的全部东西.该文件不同于单个可运行文件,后 ...

  9. Redis源代码分析(十三)--- redis-benchmark性能測试

    今天讲的这个是用来给redis数据库做性能測试的,说到性能測试,感觉这必定是高大上的操作了.redis性能測试.測的究竟是哪方面的性能,怎样測试,通过什么指标反映此次測试的性能好坏呢.以下我通过源代码 ...

随机推荐

  1. 【php】查看扩展的版本

    php --ri [扩展名称] 例如: php --ri mongodb mongodb MongoDB support => enabled MongoDB extension version ...

  2. 02.28 day03

    print(1 or 3 > 2 and 4 < 5 or 6 and 2 < 7)## while True:# print(11)# print(22)# # break# # ...

  3. 【讲●解】KMP算法

    KMP算法 我们小组负责讲这个... 术语与规定 为了待会方便,所以不得不做一些看起来很拖沓的术语,但这些规定能让我们更好地理解\(KMP\)甚至\(AC\)自动机. 字符串匹配形式化定义如下: 假设 ...

  4. C指针计算字符串长度

    #include <stdio.h> int stringLength (const char *string) { const char *cptr = string; while ( ...

  5. 查看mysql二进制日志报错问题

    在排查网站被黑时想通过Mysql二进制日志找出修改字段时间,但是使用mysqlbinlog报错: [root@zfszsw1 bin]# ./mysqlbinlog /opt/mysql-bin.00 ...

  6. markdown pad激活

    <iframe src="></iframe> ---恢复内容开始--- 注册码 Soar360@live.com GBPduHjWfJU1mZqcPM3BikjYK ...

  7. perl学习之:package and module

    perl的包(package)和模块(PM) ==================================包package===========================     pac ...

  8. Mybatis 缓存策略

    听极客学院笔记 使用mybatis的缓存需要以下三步 一.在mybatis的config.xml中开启缓存 <settings> <setting name="cacheE ...

  9. POJ-2590-Steps题目详解,思路分析及代码,规律题,重要的是找到规律~~

    Steps Time Limit: 1000MS   Memory Limit: 65536K       http://poj.org/problem?id=2590 Description One ...

  10. PTA 03-树2 List Leaves (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/666 5-4 List Leaves   (25分) Given a tree, you ...