遍历一个数组看谁快

参赛选手 ForLoopFor - inenumerateObjectsUsingBlock这个三个方法:

    NSMutableArray *test = [NSMutableArray array];
for (int i = 0; i < 1000000; i ++) {
[test addObject:@(i)];
} __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); 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); 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);

最后输出如下:

打印输出
  • For-in方法最快速

    结论:

    当只是遍历一个数组的时候使用For-in会比较快速, 推荐使用For-in遍历数组.

通过Value查找Index看谁快

假如现在我们要查找一个Value, 这个Value 值是100001, 找出它的index (数组的序列号).

那么现在我们来比较一下
参赛选手 For - inenumerateObjectsUsingBlockenumerateObjectsWithOptions 这个三个方法: (ForLoop已经不再继续讨论了)

    NSMutableArray *test = [NSMutableArray array];
for (int i = 0; i < 10000000; 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);

最后输出如下图:

打印输出
  • enumerateObjectsWithOptions方法最快速

    结论:

    通过Value查询index的时候, 面对大量的数组推荐使用 enumerateObjectsWithOptions的并行方法.
    For-inenumerateObjectsWithOptions方法这里我比较喜欢第二种写法简洁直观.

现在咱们要遍历字典

这里我们比较一下使用 For-in 和 enumerateKeysAndObjectsUsingBlock 这个两个方法:

    NSDictionary *testDictionary = @{
@"Auther" : @"yyyyy",
@"Game" : @"Dota",
@"App" : @"dddddd",
@"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 For-in Time: %f ms",(long)index,date_current * 1000); NSLog(@"ForInArr: %@",forInArry);
NSLog(@"enumArry: %@",enumArry);

打印输出:

打印输出
  • enumerateKeysAndObjectsUsingBlock胜出

    结论:

    当我们想遍历字典类型的时候, 推荐使用enumerateKeysAndObjectsUsingBlock
    不仅仅是因为速度快, 更是因为代码更优雅和直观.

关于EnumerateObjectsUsingBlock和for-in之间的较量的更多相关文章

  1. Unity3d之MiniJson与LitJson之间的较量

    由于项目不得不用到json来解析服务器端传来的数据,于是不得不选择一种在unity3d上面可用的json.开始根据网上推荐LitJson,于是下载下来源码,导入项目: 经过测试可以用:但是移植到ipa ...

  2. iOS数组使用

    相关链接: ios数组基本用法和排序 NSArray 排序汇总 iOS 数组排序方法 IOS-筛选数组内的元素 关于EnumerateObjectsUsingBlock和for-in之间的较量 [iO ...

  3. 我是如何在SQLServer中处理每天四亿三千万记录的

    首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地方,请各位数据库大牛给予指正,以便我能够更好的处理此次业务. ...

  4. 如何选择PHP框架?

    PHP是世界上最受欢迎的编程语言之—.最近发布的PHP7令这种服务器的编程语言比以前变得更好,更稳定了. PHP被广泛应用于重大的项目.例如Facebook就是使用PHP来维护和创建它们的内部系统的. ...

  5. 【转】我是如何在SQLServer中处理每天四亿三千万记录的

    原文转自:http://blog.jobbole.com/80395/ 首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文 ...

  6. 给swift程序猿留下深刻印象的10个Swift代码

    通过使用单行代码完成同样的 10 个练习,我们来看看 Swift 和其他语言之间的较量. 将数组中每个元素的值乘以 2 使用map来实现 var arr = [1,2,3,4]; var newArr ...

  7. 如何在SQLServer中处理每天四亿三千万记录

    首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地方,请各位数据库大牛给予指正,以便我能够更好的处理此次业务. ...

  8. (转)SqlServer中处理每天四亿三千万记录的

    项目背景 这是给某数据中心做的一个项目,项目难度之大令人发指,这个项目真正的让我感觉到了,商场如战场,而我只是其中的一个小兵,太多的战术,太多的高层之间的较量,太多的内幕了.具体这个项目的情况,我有空 ...

  9. (转)我是如何在SQLServer中处理每天四亿三千万记录的

    首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地方,请各位数据库大牛给予指正,以便我能够更好的处理此次业务. ...

随机推荐

  1. 【云计算】ubuntu下docker安装配置指南

    Docker Engine安装配置 以下描述仅Docker在Ubuntu Precise 12.04 (LTS).Ubuntu Trusty 14.04 (LTS).Ubuntu Wily 15.10 ...

  2. There is no getter for property named 'purchaseApplyId' in 'class java.lang.Long'

    mapper.xml: <delete id="deleteByPurchaseAppyId" parameterType="Long"> < ...

  3. windows下安装Appserv等php套件之后无法进入数据库管理的问题

    在win7下安装Wamp或者Appserv后无法进入数据库管理,但是php.Apache运行全都没问题,mysql可以在命令行中管理,但是就是无法打开phpmyadmin数据库管理,点击后浏览器就显示 ...

  4. 2.saltstack笔记之目标,模块,返回写入数据库

    作者:刘耀 QQ:22102107 一.目标(targeting Minions) 1.匹配Minions Id 匹配所有 (*) [root@node1 salt]# salt '*' test.p ...

  5. iOS图形处理和性能(转)

    在之前的文章里,我们探讨了基于多种不同技术来实现自定义的UIButton,当然不同的技术所涉及到的代码复杂度和难度也不一样.但是我也有意提到了基于不同方法的实现所体现出的性能表现也不一一相同.   [ ...

  6. fedora 添加其他操作系统到 GRUB 2 菜单

    # yum install os-prober # grub2-mkconfig -o /boot/grub2/grub.cfg

  7. Android之ExpandableListView

    ExpandableListView可以用来表现多层级的listView,本文主要是ExpandableListView的一个简单实现 布局文件 <LinearLayout xmlns:andr ...

  8. KVM中Linux虚拟机的硬盘添加方法

    [root@cache01 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_roo ...

  9. LeetCode之LRU Cache 最近最少使用算法 缓存设计

    设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...

  10. 如何将win7安装到 移动硬盘/U盘 及 VHD、BCD等相关知识 链接汇总

    如何将win7安装到 移动硬盘/U盘? 从u盘启动win7/2008 R2的方法,更新:换机器免sysprep的方法:http://www.360doc.com/content/11/1103/14/ ...