I did some runs on my iOS app with Instruments and I saw that 90% of the load on the main thread on launch (about 1000ms total) is caused by containsObject: calls. That’s on the main thread and I don’t think it’s cool.
Is there a faster alternative to this method? An algorithm or another method?
Any suggestions?
MORE INFO:
-
I looked into my code again I realized that in fact I don’t need to know the order of the objects, only if an object is part of that set. Which means NSSet will do just fine (and I guess is faster).
-
Number of objects – there may very well be 1000+ objects in that set.
If you NEED to use an array, skip a bit further down
Alternate Options
Your other options might include:
Using an
NSDictionarywhich uses key->value pairs which (I expect will) have O(1) read complexity at the cost of extra storage space for the keysIf you aren’t using duplicates and order isn’t important, using an
NSSetwill offer better read complexity (I don’t know what the complexity will be, the docs probably will)Using an Array
If you keep the array sorted, searching can be done in
O(log n)time instead ofO(n)as you can take advantage of a binary search.Caveat Lector: This was written from memory