Is NSSet faster than NSArray?

≯℡__Kan透↙ 提交于 2019-12-12 09:13:48

问题


i am new to iOS. Anyone know if NSSet are faster than NSArray? Why wouldn't I always just use an NSArray?

Anyone explain me difference between NSSet and NSArray?


回答1:


NSSet is used to have unique objects.

NSArray may have duplicate objects.

NSSet is an unordered collection.

NSArray is an ordered collection.

NSArray is faster than NSSet for simply holding and iterating. As little as 50% faster for constructing and as much as 500% faster for iterating. if you only need to iterate contents, don't use an NSSet.




回答2:


When the order of the items in the collection is not important, sets offer better performance for finding items in the collection.

The reason is that a set uses hash values to find items (like a dictionary) while an array has to iterate over its entire contents to find a particular object.

More detailed information here:

http://www.cocoawithlove.com/2008/08/nsarray-or-nsset-nsdictionary-or.html




回答3:


When the order of elements isn’t important and performance of testing whether an object is in the set is important. Even though arrays are ordered, testing them for membership is slower than testing sets.



来源:https://stackoverflow.com/questions/22679418/is-nsset-faster-than-nsarray

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!