Speed: iOS Using NSPredicate filterUsingPredicate vs. for loop

假如想象 提交于 2019-12-10 18:14:58

问题


I need to filter an NSMutableArray of custom objects and was wondering about whether or not one of the following is BETTER than the other in terms of speed/runtime, or if they are virtually the same:

(1) Using [array filterUsingPredicate:predicate],

or

(2) Using a for loop to iterate through all elements and check if they satisfy the criteria or not myself.

I only ask this because I think the criteria each object must satisfy could vary so making the predicate could be tricky.

Thanks in advance!


回答1:


I wouldn't trust either technique to be meaningfully faster in the general case. Both techniques will be sensitive to how the loop or predicates are written, and what the data looks like. If the array is unsorted with respect to your filter criteria, they'll both just have to step through all the elements anyway.

Use the one that leaves you with cleaner and more maintainable code, and then consider trying and comparing it to the other approach if you see that there's an actual performance bottleneck.



来源:https://stackoverflow.com/questions/17887919/speed-ios-using-nspredicate-filterusingpredicate-vs-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!