Is NSSet faster than NSArray?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 18:46:08
Vijay-Apple-Dev.blogspot.com

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.

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

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.

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