如何得到带顺序的nsdictionary 遍历

Continuing off this post:
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...
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...
Any suggestions?
--If you want more details:
What I mean is easier to expl
For the array:
(for NSString *str in array) { do smth with the string }
For the dictionary
(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?
--EDIT (comment)
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...
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...
解决方案 I'd like to point you at the following article: "", by , 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).
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).
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:
NSArray, Fast Enumeration:
NSDictionary, Block Enumeration
NSDictionary, allKeys -& Fast Enumeration
NSDictionary, allValues -& Fast Enumeration
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.
So the big question is, why would you consider using a dictionary? Arrays and hash tables aren't exa 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.
本文地址: &
继续了这个帖子:Performance命中使用的NSMutableDictionary与NSMutableArray的&GT发生;
我试图运行一个小测试,看看性能差距是伟大的读取和NSArray的&放大器之间的写入;的NSDictionary以及它们的可变coutnerparts ... 不过,我有困难找到一个“平衡”的测试......因为字典有2个(或3取决于你怎么看),通过对获取值(不是键)seeked对象循环,而阵列仅具有一个... 有什么建议?
如果您想了解更多详细信息:我的意思是通过更简单的例子来解释; 有关数组:(为的NSString * STR数组){做字符串不便} 有关字典 (在的NSString * STR [字典allValues]){}字符串 或 (在[词典allKeys]的NSString * STR){[:密钥词典valueForKey]} 或 (在的NSString * STR [字典allKeys]){}字符串 甚至 的NSArray * valuesOrKeys = [字典allKeys / allValues](为的NSString * STR在valuesOrKeys){}字符串 什么是“最公平”测试的字典呢?
- 编辑(评论)大家都指出(并问我为什么会想那),当使用一本字典,那是因为它符合模型不是一个数组... 更好以及我的要求的原因是,一个应用程序,我要建不但慢,而且,所以我想弄清楚,如果使用不同的数据类型会改变任何这一点,我使用基本的C数组考虑...我有选择在这一点上,所以我能够改变的内部运作,以适应我想要的任何类型... 解决方案 我想在下面的文章给你指出:“的”,由,在苹果的一名工程师。可可阵列不一定实现良好的天真数组作为你所期望的,也不是简单的字典哈希表。他们的表现非常旁证,并且取决于他们保持的对象的数目(以及它们的值,等等)。这可能不直接影响答案,但它的东西要考虑(的NSDictionary 性能,当然,变化与你的散列函数的速度和可靠性,等等)。 此外,如果你正在寻找一个“平衡”的测试,你就必须寻找一种方式这两个类表现为彼此接近越好。你想排除通过在字典键访问值,因为 - 不论如何快速查找时间是由的NSDictionary 维护的基础数据结构 - 比单纯拉慢从一个数组对象,因为您要执行更多的操作去做。从一个数组访问是 O(1),对于一个哈希表, O(1)充其量 O(N)在最坏的情况(取决于执行,二者中间)。有几种方法可以列举两个字典和数组,你上面提到。你会想使用最靠近彼此在执行方面,这些是不是基于块的枚举( enumerateObjectsUsingBlock方法:为 NSArray的和 enumerateKeysAndObjects:为的NSDictionary ),或快速枚举(使用 allKeys 或 allValues 为的NSDictionary )。由于这些算法的性能主要是经验性的,我进行了多次试验需要注意的访问时间(每10000 的NSNumber 对象): 的NSArray座枚举:1. 10.5s2. 9.1s3. 10.0S4. 9.8s5. 9.9S
9.9S平均NSArray的,快速计数:1. 9.7s2. 9.5s3. 9.3s4. 9.1s5. 10.5s
9.6s平均的NSDictionary,枚举座1. 10.5s2. 10.6s3. 9.9S4. 11.1s5. 11.0s
10.6s平均的NSDictionary,allKeys
- >快速计数1. 10.0S2. 11.2s3. 10.2s4. 10.8s5. 10.8s
10.6s平均的NSDictionary,allValues
- >快速计数1. 10.7s2. 10.3s3. 10.5s4. 10.5s5. 9.7s
10.3s平均 你可以从这个人为测试的结果,的NSDictionary 看比的NSArray 显然较慢(约7 %使用块枚举慢,而且具有快速枚举7-10%速度较慢)。但是,这种比较是相当没有意义,看到用最快的枚举的NSDictionary 转予只是它变成一个数组反正。因此,最大的问题是,为什么你会考虑使用一本字典?数组和哈希表是不完全互换;你有什么样的模式,允许简易替换的NSArray 与的NSDictionary ?不管被人为赋予的例子来证明性能的时代益这样或那样的,你应该总是实现的方式,使的感觉你的模型的 - 你可以在以后的性能,如果你有优化。我不知道你会如何使用这些数据结构可以互换,但无论如何,的NSArray 是赢家,尤其是考虑到在其中您试图访问值的顺序
本文地址: &
扫一扫关注官方微信保持的NSDictionary键的顺序,当转换到NSData的与[NSJSONSerialization dataWithJSONObject:]-ios,cocoa-touch-CodeGo.net
保持的NSDictionary键的顺序,当转换到NSData的与[NSJSONSerialization dataWithJSONObject:]
我有以下的情况:
NSDictionary *params = @{
@"Checkout" : @{
@"conditions" : @{@"Checkout.user_id" : @1},
@"order" : @{@"Checkout.id" : @"DESC"}
@"PaymentState" : @[],
@"Offer" : @[]
这本词典包含PARAMS的web服务请求传递JSON字符串与Web服务的URL。我得到的JSON NSJSONSerialization类,像这样:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
问题是:jsonString“钥匙”是从原来的params dictionary中的键顺序,这样的排序是不同的:
"Offer":[],
"PaymentState":[],
"Checkout":{
"conditions":{"Checkout.user_id":6},
"order":{"Checkout.id":"DESC"}
也就是说,先在jsonString的“和”要约“键,我需要保持
原来的顺序。这是非常重要的,就像这样:
"Checkout":{
"conditions":{"Checkout.user_id":6},
"order":{"Checkout.id":"DESC"}
"Offer":[],
"PaymentState":[]
所以啊,我该怎么做?
本文地址 :CodeGo.net/9434923/
-------------------------------------------------------------------------------------------------------------------------
1.OrderedDictionary从CocoaWithLove每当我需要保持我的字典键的顺序。
基本上,OrderedDictionary包含一个NSMutableDictionary和NSMutableArray为它里面的密钥,以保持轨道的键的顺序。它的子类NSDictionary/NSMutableDictionary而只是“通过”打电话到内部NSMutableDictionary
根据JSON规范的JSON对象是专门无序.eachJSON库是要考虑到这一点。所以,即使你解决这个问题,现在,你几乎肯定会在以后遇到的问题;你正在做的不成立(即按键排序)的假设。
本文标题 :保持的NSDictionary键的顺序,当转换到NSData的与[NSJSONSerialization dataWithJSONObject:]
本文地址 :CodeGo.net/9434923/
Copyright (C) 2014 CodeGo.net

我要回帖

更多关于 nsdictionary 的文章

 

随机推荐