关于EnumerateObjectsUsingBlock和for-in之间的较量
遍历一个数组看谁快
参赛选手 ForLoop, For - in, enumerateObjectsUsingBlock这个三个方法:
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 - in, enumerateObjectsUsingBlock, enumerateObjectsWithOptions 这个三个方法: (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-in和enumerateObjectsWithOptions方法这里我比较喜欢第二种写法简洁直观.
现在咱们要遍历字典
这里我们比较一下使用 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之间的较量的更多相关文章
- Unity3d之MiniJson与LitJson之间的较量
由于项目不得不用到json来解析服务器端传来的数据,于是不得不选择一种在unity3d上面可用的json.开始根据网上推荐LitJson,于是下载下来源码,导入项目: 经过测试可以用:但是移植到ipa ...
- iOS数组使用
相关链接: ios数组基本用法和排序 NSArray 排序汇总 iOS 数组排序方法 IOS-筛选数组内的元素 关于EnumerateObjectsUsingBlock和for-in之间的较量 [iO ...
- 我是如何在SQLServer中处理每天四亿三千万记录的
首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地方,请各位数据库大牛给予指正,以便我能够更好的处理此次业务. ...
- 如何选择PHP框架?
PHP是世界上最受欢迎的编程语言之—.最近发布的PHP7令这种服务器的编程语言比以前变得更好,更稳定了. PHP被广泛应用于重大的项目.例如Facebook就是使用PHP来维护和创建它们的内部系统的. ...
- 【转】我是如何在SQLServer中处理每天四亿三千万记录的
原文转自:http://blog.jobbole.com/80395/ 首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文 ...
- 给swift程序猿留下深刻印象的10个Swift代码
通过使用单行代码完成同样的 10 个练习,我们来看看 Swift 和其他语言之间的较量. 将数组中每个元素的值乘以 2 使用map来实现 var arr = [1,2,3,4]; var newArr ...
- 如何在SQLServer中处理每天四亿三千万记录
首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地方,请各位数据库大牛给予指正,以便我能够更好的处理此次业务. ...
- (转)SqlServer中处理每天四亿三千万记录的
项目背景 这是给某数据中心做的一个项目,项目难度之大令人发指,这个项目真正的让我感觉到了,商场如战场,而我只是其中的一个小兵,太多的战术,太多的高层之间的较量,太多的内幕了.具体这个项目的情况,我有空 ...
- (转)我是如何在SQLServer中处理每天四亿三千万记录的
首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地方,请各位数据库大牛给予指正,以便我能够更好的处理此次业务. ...
随机推荐
- Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- jquery validate自定义checkbox验证规则和样式
参考:http://blog.csdn.net/xh16319/article/details/9987847 自定义checkbox验证,“检查checkbox是否选中” jQuery.valida ...
- mysql 源码下载列表
2013年11月16日 14:57:04 http://download.softagency.net/MySQL/Downloads/MySQL-5.5/ http://code.google.co ...
- ShortestPath:Wormholes(POJ 3259)
田里的虫洞 题目大意:就是这个农夫的田里有一些虫洞,田有很多个点,点与点之间会存在路,走过路需要时间,并且这些点存在虫洞,可以使农夫的时间退回到时间之前,问你农夫是否真的能回到时间之前? 读完题:这一 ...
- codeforces 485A.Factory 解题报告
题目链接:http://codeforces.com/problemset/problem/485/A 题目意思:给出 a 和 m,a 表示第一日的details,要求该日结束时要多生产 a mod ...
- JavaScript设计模式 - 代理模式
代理模式是为一个对象提供一个代用品或占位符,以便控制对它的访问 代理模式的用处(个人理解):为了保障当前对象的单一职责(相对独立性),而需要创建另一个对象来处理调用当前对象之前的一些逻辑以提高代码的效 ...
- jquery记住密码,记住账号,自动登录
1.引入jquery库 2.引入jquery.cookie.js库 3.引入操作js jsp如下: $(document).ready(function() { //输入框获得焦点-失去焦点 $(&q ...
- [Android Pro] Android 之使用LocalBroadcastManager解决BroadcastReceiver安全问题
参考博客: http://blog.csdn.net/t12x3456/article/details/9256609 http://blog.csdn.net/lihenair/article/de ...
- July 24th, Week 31st Sunday, 2016
Miracles happen every day. 奇迹每天都在发生. Miracles actually happen every day, every moment. You may think ...
- ip数据结构
本文摘自 linux kernel ip.h,感谢开源的GNU struct ip { #if __BYTE_ORDER == __LITTLE_ENDIAN unsigned int ip_hl:4 ...