I am not at all sure that using the predicate to filter out a subset is any faster from the primitive loop suggested beforehand.
It is a more concise and beautiful code, but by no means faster. Here are a few reasons (overheads) I can see immediately.
- Creating and compiling a predicate from a text format takes both time and memory (several allocations)
- Filtering the self.hasExpenditures using predicate again allocates and initiates a new NSSet (shortSet) and populates it with (retained) references to the matching expenditures (withn the date range). For that it must scan the self.expenditures one by one in a loop.
- Then the last total calculations, again loops over the subset summing over amount, and allocating the final NSNumber object.
In the loop version --- there is no new allocation, no retaining nor releasing of anything, and just one pass over the self.expenditures set.
All in all, my point is, the second implementation will NEED to do AT LEAST the contents of that loop anyway, plus some more overheads.
And a last point: for id in collection can run concurrently using GCD on several items, hence it is quite fast.
I think you should at least try to match these alternatives via extensive performance test.