abisson
abisson

Reputation: 4425

In CoreData, should we always convert an NSSet to NSArray for iteration?

So in my Core Data relationships, I have an object with a relationship to other objects that is mapped via an NSSet. When I get my primary object, I will want to iterate over the NSSet. Just iteration, nothing fancy.

Now, according to this article, NSArray is much faster than NSSet. http://www.cocoawithlove.com/2008/08/nsarray-or-nsset-nsdictionary-or.html

My NSSet will never be that big, so I don't think it really matters in this case. However, I just wanted to know in general, is the overhead of converting a NSSet to NSArray for iteration still faster overall?

Thanks!

Upvotes: 0

Views: 772

Answers (1)

Chris Devereux
Chris Devereux

Reputation: 5473

Converting an NSSet to an NSArray will require iteration over the set, as well as allocating memory for the array so just iterating over the set will be faster unless you're going to iterate over it repeatedly.

But in either case, even with quite a large set, we're likely talking nanoseconds. I really wouldn't worry about it unless you've profiled an actual performance issue and the data tells you its a problem.

Also, the performance figures you've linked only really apply for collections you create yourself. When you get a collection back from a framework, it can (and in the case of core data, will) return you a collection with a custom implementation that has its own performance characteristics.

Upvotes: 2

Related Questions