Performance wise, on a relatively large array (so far the usual count for the original array is ±20000), which method is best suited to filter it? Blocks or predicates?
Most of the ivars of the contained objects are strings and I want to query those.
There is one way that blocks could be faster:
NSEnumerationConcurrent
to enumerate the array.However, the documentation doesn't explicitly say that order will be preserved when enumerating concurrently. I think it's a good bet that it won't be. If the order of the array matters, you'd have to re-sort (if that's even possible), and you'd have to include that in any timing comparison.
The other ways are to non-concurrently enumerate using blocks and to filter using predicates. filterUsingPredicate:
could be faster, since NSArray will have the opportunity to use internal knowledge to build the result array faster than repeated addObject:
messages. But that's merely a possibility; the only way to know for sure would be to compare, and even then, the answer could change at any time (including in the same process, for different input arrays or different objects in the array).
My advice would be to implement it straightforwardly—using predicates—at first, and then use Instruments to see whether it's a performance problem. If not, clear code wins. If it is a performance problem, try concurrent enumeration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With