简体   繁体   English

为什么使用NSArray而不是NSOrderedSet

[英]Why use NSArray instead of NSOrderedSet

In many good articles online, including this one: http://nshipster.com/nsorderedset/ it is stated that we should still prefer NSArray over NSOrderedSet in general. 在许多在线优秀文章中,包括以下文章: http : NSOrderedSet都指出,与NSOrderedSet ,我们还是应该更喜欢NSArray

An advantage that i've noticed is the syntactic sugar of NSArray : @[obj1, obj2, ...] 我注意到的一个优点是NSArray的语法糖: @[obj1, obj2, ...]

So what are the other advantages of NSArray that makes it the preferred collection manager instead of NSOrderedSet , which seems to have a lot of benefits (fast access to elements), and it basically includes the NSArray functionality? 那么,使NSArray成为首选的集合管理器而不是NSOrderedSet的另一个优点是什么( NSOrderedSet似乎有很多好处(快速访问元素),并且基本上包括NSArray功能)?

I timed inserting 1,000,000 random NSNumbers and found that NSMutableOrderedSet was about 7 to 10 times slower. 我定时插入1,000,000个随机NSNumbers ,发现NSMutableOrderedSet慢了大约7到10倍。 With fewer items the times became closer, at 1000 items the times were about 2.5 times slower. 物品越少,时间就越近,而物品1000时,速度要慢2.5倍。 Access times were close. 访问时间接近。

These times represent about the best for NSMutableOrderedSet that I saw: 这些时间代表了我所看到的NSMutableOrderedSet的最佳状态:

iterations: 1000000 迭代次数:1000000

NSMutableOrderedSet: 782 msec NSMutableOrderedSet:782毫秒
count: 999868 数:999868

NSMutableArray: 112 msec NSMutableArray:112毫秒
count: 1000000 数:1000000

Note that there were 132 duplicates that were rejecter by NSMutableOrderedSet . 请注意,有132个重复项被NSMutableOrderedSet拒绝。

Array 数组

 It is ordered index collection.
 It allows duplicate.

Set

 It is unordered collection.
 It does not allow duplicate.It is unique.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM