Difference in lambda performances?

前端 未结 3 1968
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 20:33

This is not a duplicate of my question. I checked it and it is more about inner anonymous classes.

I was curious about Lambda expressions and tested the following :

3条回答
  •  伪装坚强ぢ
    2021-01-02 21:18

    I think the performance difference you're seeing is probably due more to removeIf's use of an iterator internally vs. get and remove in your for loop. The answers in this PAQ have some good information on the benefits of iterators.

    bayou.io's answer is spot on, you can see the code for removeIf here it does two passes to avoid shifting the remaining elements over and over.

提交回复
热议问题