Vasavi
Vasavi

Reputation: 49

Is NSSet faster than NSArray?

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?

Upvotes: 1

Views: 8237

Answers (3)

NSSet is used to have unique objects.

NSArray may have duplicate objects.

NSSet is an unordered collection.

NSArray is an ordered collection.

enter image description here

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.

Upvotes: 16

Harish Suthar
Harish Suthar

Reputation: 711

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.

Upvotes: 0

gagarwal
gagarwal

Reputation: 4244

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

Upvotes: 0

Related Questions