做app的时候,总免不了要多次遍历数组或者字典。
究竟哪种遍历方式比较快呢?我做了如下测试:
首先定义测试用宏:

1
2
3
4
5
6
7
8
9
#define
MULogTimeintervalBegin(INFO) NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];\
NSTimeInterval

duration = 0;\
NSLog(@"MULogTimeintervalBegin:%@",
INFO)
 
#define
MULogTimeintervalPauseAndLog(INFO) duration = [NSDate timeIntervalSinceReferenceDate] - start;\
start
+= duration;\
NSLog(@"%@:%f",
INFO, duration);\
duration
= 0
#define
TESTSCALE 100000

接着编写测试代码:
NSarray:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-
(
void)testArray
{
    NSMutableArray*
testArray = [
NSMutableArray

arrayWithCapacity:TESTSCALE];
    for

(
NSInteger

i = 1; i <= TESTSCALE; ++i) {
        [testArray
addObject:[
NSString

stringWithFormat:@
"%ld",
i]];
    }
    NSLog(@"init:%ld",
[testArray count]);
     
    __block
NSMutableString*
sum = [
NSMutableString

stringWithCapacity:TESTSCALE];
     
    MULogTimeintervalBegin(@"ArrayTest");
    NSUInteger

count = [testArray count];
    for

(
NSInteger

i = 0; i < count; ++i) {
        [sum
appendString:[testArray objectAtIndex:i]];
    }
    [sum
setString:@
""];
    MULogTimeintervalPauseAndLog(@"for
statement"
);
     
    for(NSString*
item in testArray) {
        [sum
appendString:item];
    }
    [sum
setString:@
""];
    MULogTimeintervalPauseAndLog(@"for-in");
     
    [testArray
enumerateObjectsUsingBlock:^(
id

obj,
NSUInteger

idx,
BOOL

*stop) {
        [sum
appendString:obj];
    }];
    [sum
setString:@
""];
    MULogTimeintervalPauseAndLog(@"enumerateBlock");
}

NSDictionary:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-
(
void)testDictionary
{
    NSMutableDictionary*
testDic = [
NSMutableDictionary

dictionaryWithCapacity:TESTSCALE];
    for

(
NSInteger

i = 1; i <= TESTSCALE; ++i) {
        [testDic
setObject:@
"test"

forKey:[
NSString

stringWithFormat:@
"%ld",
i]];
    }
    NSLog(@"init:%ld",
[testDic count]);
     
    __block
NSMutableString*
sum = [
NSMutableString

stringWithCapacity:TESTSCALE];
     
    MULogTimeintervalBegin(@"DictionaryTest");
    for

(
NSString*
object in [testDic allValues]) {
        [sum
appendString:object];
    }
    [sum
setString:@
""];
    MULogTimeintervalPauseAndLog(@"for
statement allValues"
);
     
    for

(
id

akey in [testDic allKeys]) {
        [sum
appendString:[testDic objectForKey:akey]];
    }
    [sum
setString:@
""];
    MULogTimeintervalPauseAndLog(@"for
statement allKeys"
);
     
    [testDic
enumerateKeysAndObjectsUsingBlock:^(
id

key,
id

obj,
BOOL

*stop) {
        [sum
appendString:obj];
    }
];
    MULogTimeintervalPauseAndLog(@"enumeration");
}

下面是测试结果:
Test Case '-[LoopTestTests testArray]' started.
2012-08-02 17:14:22.061 otest[388:303] init:100000
2012-08-02 17:14:22.062 otest[388:303] MULogTimeintervalBegin:ArrayTest
2012-08-02 17:14:22.075 otest[388:303]for statement:0.013108
2012-08-02 17:14:22.083 otest[388:303]for-in:0.008186
2012-08-02 17:14:22.095 otest[388:303] enumerateBlock:0.012290
Test Case '-[LoopTestTests testArray]' passed (0.165 seconds).
Test Case '-[LoopTestTests testDictionary]' started.
2012-08-02 17:14:22.273 otest[388:303] init:100000
2012-08-02 17:14:22.274 otest[388:303] MULogTimeintervalBegin:DictionaryTest
2012-08-02 17:14:22.284 otest[388:303] for statement allValues:0.010566
2012-08-02 17:14:22.307 otest[388:303] for statement allKeys:0.022377
2012-08-02 17:14:22.330 otest[388:303] enumeration:0.023914
Test Case '-[LoopTestTests testDictionary]' passed (0.217 seconds).

可以看出对于数组来说,for-in方式遍历速度是最快的,普通风格的for和block方式速度差不多。对于字典来说,allValues方式遍历最快,allKeys和block差不多。
那么,为什么会这样呢?
NSArray:

1
2
3
for

(
NSInteger

i = 0; i < count; ++i) {
        [sum
appendString:[testArray objectAtIndex:i]];
}

这里由于存在:[objectAtIndex:i]这样的取操作,所以速度会有所下降。

1
2
3
for(NSString*
item in testArray) {
        [sum
appendString:item];
}

尽管也有取操作,但是绕开了oc的message机制,速度会快一点。也有可能是编译器为了for-in作了优化。
block为什么会慢一些这个有待研究。
NSDictionary:

1
2
3
for

(
id

akey in [testDic allKeys]) {
        [sum
appendString:[testDic objectForKey:akey]];
}

这个就很明显了,第二种方法多了一次objectForKey的操作。block的话有待研究。


google了一下,stackoverflow上面有类似的讨论:点击打开链接
大意是:for-in语法会对容器里面的元素的内存地址建立一个缓冲,遍历的时候从缓冲直接取得元素的地址而不是通过调用方法来获取,所以效率比较高。另外,这也是不能在循环体中修改容器元素的原因之一。

oc/object-c/ios哪种遍历NSArray/NSDictionary方式快?测试报告的更多相关文章

  1. iOS五种本地缓存数据方式

    iOS五种本地缓存数据方式   iOS本地缓存数据方式有五种:前言 1.直接写文件方式:可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...

  2. 遍历NSArray, NSDictionary, NSSet的方法总结

    1,for循环读取 NSArray: NSArray *array = /*…*/ ; i<array.count; i++) { id object = array[i]; // do sth ...

  3. HashMap两种遍历数据的方式

    HashMap的遍历有两种方式,一种是entrySet的方式,另外一种是keySet的方式. 第一种利用entrySet的方式: Map map = new HashMap(); Iterator i ...

  4. iOS - 数组与字典(NSArray & NSDictionary)

    1. 数组的常用处理方式 //--------------------不可变数组 //1.数组的创建 NSString *s1 = @"zhangsan"; NSString *s ...

  5. [集合]Map的 entrySet() 详解以及用法(四种遍历map的方式)

    Entry 由于Map中存放的元素均为键值对,故每一个键值对必然存在一个映射关系. Map中采用Entry内部类来表示一个映射项,映射项包含Key和Value (我们总说键值对键值对, 每一个键值对也 ...

  6. IOS四种保存数据的方式

    在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: 1.NSKeyed ...

  7. IOS 四种保存数据的方式

    在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: 1.NSKeyed ...

  8. Java中五种遍历HashMap的方式

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Java8Templat ...

  9. 另一种遍历Map的方式: Map.Entry 和 Map.entrySet()

    源网址: http://blog.csdn.net/mageshuai/article/details/3523116 今天看Think in java 的GUI这一章的时候,里面的TextArea这 ...

随机推荐

  1. for 、forEach 、 forof、 forin遍历对比

    一.遍历内容的异同 1.for 和 for...in 是针对数组下标的遍历 2.forEach 及 for...of 遍历的是数组中的元素 二.对非数字下标的处理 由于array在js中也是对象中的一 ...

  2. c++各种排序的简单实现

    /* 直插排序 */ void InsertSort(vector<int> &arr){ for(int i = 1;i < arr.size();++i){ for(in ...

  3. php 全文搜索解决方法

    全套解决方案 xunsearch 一.安装编译工具 yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-dev ...

  4. Webcollector应用(二)

    先吐槽一句哀家的人品,总在写好代码之后,网站默默的升级,没有一点点防备... 一.加代理 爬取一个网站的时候,爬了不到一半,IP被封了,整个内部局域网的所有电脑都不能访问网站了. public cla ...

  5. C# String.Format用法和格式说明

    1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...

  6. ifconfig与内核通信 ifreq 结构体分析和使用

    结构原型: /* * Interface request structure used for socket * ioctl's.  All interface ioctl's must have p ...

  7. 产生随机数 random

    int rand(void); 返回 0 ------- RAND_MAX 之间的一个 int 类型整数,该函数为非线程安全函数.并且生成随机数的性能不是很好,已经不推荐使用.        void ...

  8. HDU 3746 Cyclic Nacklace(KMP找循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题目大意:给你一个字符串,求出将字符串的最少出现两次循环节需要添加的字符数. 解题思路: 这题需 ...

  9. CentOS6.9下安装MariaDB10.2.11

    yum groupinstall -y "Development Tools" yum install -y cmake openssl-devel zlib-devel yum ...

  10. html学习-css

    1.css初识 css 中文解释:层叠样式表,把html比作骨骼的话,css就是衣服,他的外在都能通过css来修饰,js则是肌肉,能使html动起来.产生用户交互... 1.1css样式表类型 css ...