问题
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