中的NSDictionary的NSArray VS性能读取 [英] performance for reads of nsdictionary vs nsarray

查看:401
本文介绍了中的NSDictionary的NSArray VS性能读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续了这个帖子:<一href=\"http://stackoverflow.com/questions/1098174/performance-hit-incurred-using-nsmutabledictionary-vs-nsmutablearray\">Performance命中使用的NSMutableDictionary与NSMutableArray的&GT发生;

我试图运行一个小测试,看看性能差距是伟大的读取和NSArray的&放大器之间的写入;的NSDictionary以及它们的可变coutnerparts ...

I am trying to run a little test to see if the performance gap is that great for read and writes between NSArray & NSDictionary as well as their mutable coutnerparts...

不过,我有困难找到一个平衡的测试......因为字典有2个(或3取决于你怎么看),通过对获取值(不是键)seeked对象循环,而阵列仅具有一个...

However, I am having difficulties finding a "balanced" test... because the dictionary has 2 (or 3 depending on how you see this) objects to loop through to get the value (not the key) seeked, while the array has only one...

有什么建议?

- 如果您想了解更多详细信息:
我的意思是通过更简单的例子来解释;

--If you want more details: What I mean is easier to explain through examples;

有关数组:
(为的NSString * STR数组){做字符串不便}

For the array: (for NSString *str in array) { do smth with the string }

有关字典

(for NSString *str in [dictionary allValues]) { string }

(for NSString *str in [dictionary allKeys]) { [dictionary valueForKey:key] }

(for NSString *str in [dictionary allKeys]) { string }

甚至

NSArray *valuesOrKeys = [dictionary allKeys/allValues];

(for NSString *str in valuesOrKeys) {string }

什么是最公平测试的字典呢?

What is the "fairest" test to do for the dictionary?

- 编辑(评论)

大家都指出(并问我为什么会想那),当使用一本字典,那是因为它符合模型不是一个数组...

As you all pointed (and asked why I would want that) that when a dictionary is used, it's because it fits the model better than an array...

以及我的要求的原因是,一个应用程序,我要建不但慢,而且,所以我想弄清楚,如果使用不同的数据类型会改变任何这一点,我使用基本的C数组考虑...我有选择在这一点上,所以我能够改变的内部运作,以适应我想要的任何类型...

well the reason for my asking is that an app I'm building is painfully slow and so I'm trying to figure out if the use of a different datatype would change any of that, and I am considering using basic c arrays... I have the choice at this point so I am able to change the inner workings to fit whatever type I want...

推荐答案

我想在下面的文章给你指出:的阵列,由 ridiculous_fish ,在苹果的一名工程师。可可阵列不一定实现良好的天真数组作为你所期望的,也不是简单的字典哈希表。他们的表现非常旁证,并且取决于他们保持的对象的数目(以及它们的值,等等)。这可能不直接影响答案,但它的东西要考虑(的NSDictionary 性能,当然,变化与你的散列函数的速度和可靠性,等等)。

I'd like to point you at the following article: "Array", by ridiculous_fish, an engineer at Apple. Cocoa arrays are not necessarily well-implemented naïve arrays as you might expect, nor are dictionaries simple hash tables. Their performance is very circumstantial, and depends on the number of objects they hold (as well as their values, etc.). This might not directly affect the answer, but it's something to consider (NSDictionary performance will, of course, vary with the speed and reliability of your hashing function, and so on).

此外,如果你正在寻找一个平衡的测试,你就必须寻找一种方式这两个类表现为彼此接近越好。你想排除通过在字典键访问值,因为 - 不论如何快速查找时间是由的NSDictionary 维护的基础数据结构 - 比单纯拉慢从一个数组对象,因为您要执行更多的操作去做。从一个数组访问是 O(1),对于一个哈希表, O(1)充其量 O(N)在最坏的情况(取决于执行,二者中间)。

Additionally, if you're looking for a 'balanced' test, you'd have to look for a way for both classes to behave as close to each other as possible. You want to rule out accessing values via keys in the dictionary, because that — regardless of how fast seek times are for the underlying data structures maintained by NSDictionary — is slower than simply pulling objects from an array because you're performing more operations to do it. Access from an array is O(1), for a hash table, O(1) at best and O(n) at worst (depending on the implementation, somewhere in the middle).

有几种方法可以列举两个字典和数组,你上面提到。你会想使用最靠近彼此在执行方面,这些是不是基于块的枚举( enumerateObjectsUsingBlock方法: NSArray的 enumerateKeysAndObjects:的NSDictionary ),或快速枚举(使用 allKeys allValues​​ 的NSDictionary )。由于这些算法的性能主要是经验性的,我进行了多次试验需要注意的访问时间(每10000 的NSNumber 对象):

There are several ways to enumerate both dictionaries and arrays, as you mentioned above. You're going to want to use the methods that are closest to each other in terms of implementation, those being either block-based enumeration (enumerateObjectsUsingBlock: for NSArray and enumerateKeysAndObjects: for NSDictionary), or fast enumeration (using either allKeys or allValues for the NSDictionary). Because the performance of these algorithms is mainly empirical, I performed several tests to note access times (each with 10000 NSNumber objects):

NSArray, Block Enumeration:
1. 10.5s
2.  9.1s
3. 10.0s
4.  9.8s
5.  9.9s
   -----
    9.9s Avg

NSArray, Fast Enumeration:
1.  9.7s
2.  9.5s
3.  9.3s
4.  9.1s
5. 10.5s
   -----
    9.6s Avg

NSDictionary, Block Enumeration
1. 10.5s
2. 10.6s
3.  9.9s
4. 11.1s
5. 11.0s
   -----
   10.6s Avg

NSDictionary, allKeys -> Fast Enumeration
1. 10.0s
2. 11.2s
3. 10.2s
4. 10.8s
5. 10.8s
   -----
   10.6s Avg

NSDictionary, allValues -> Fast Enumeration
1. 10.7s
2. 10.3s
3. 10.5s
4. 10.5s
5.  9.7s
   -----
   10.3s Avg

你可以从这个人为测试的结果,的NSDictionary 看比的NSArray 显然较慢(约7 %使用块枚举慢,而且具有快速枚举7-10%速度较慢)。但是,这种比较是相当没有意义,看到用最快的枚举的NSDictionary 转予只是它变成一个数组反正。

As you can see from the results of this contrived test, NSDictionary is clearly slower than NSArray (around 7% slower using block enumeration, and 7–10% slower with fast enumeration). However, this comparison is rather pointless, seeing as using the fastest enumeration for NSDictionary simply devolves it into an array anyway.

因此​​,最大的问题是,为什么你会考虑使用一本字典?数组和哈希表是不完全互换;你有什么样的模式,允许简易替换的NSArray 的NSDictionary ?不管被人为赋予的例子来证明性能的时代益这样或那样的,你应该总是实现的方式,使的感觉你的模型的 - 你可以在以后的性能,如果你有优化。我不知道你会如何使用这些数据结构可以互换,但无论如何,的NSArray 是赢家,尤其是考虑到在其中您试图访问值的顺序

So the big question is, why would you consider using a dictionary? Arrays and hash tables aren't exactly interchangeable; what kind of model do you have that allows drop-in replacement of NSArray with NSDictionary? Regardless of the times given by contrived examples to prove performance benefits one way or another, you should always implement your models in a way that makes sense — you can optimize later for performance if you have to. I don't see how you would uses these data structures interchangeably, but anyway, NSArray is the winner here, especially considering the sequential order in which you're attempting to access values.

这篇关于中的NSDictionary的NSArray VS性能读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆