Which is faster: clear collection or instantiate new

后端 未结 8 930
遇见更好的自我
遇见更好的自我 2020-12-01 06:26

I have some number of generic lists in my code, that have tens or hundreds elements. Sometimes I need to refill this lists with other objects, so question is: what will be f

8条回答
  •  情歌与酒
    2020-12-01 06:44

    I've made several tests for myself. Results (speed) are:

    • for small lists - eg 3 items, its faster to create new lists, but the difference is not big
    • for 10 or more items on average it is better to clear lists. For value types much better (eg 3-4 times), for value times like 20% better.

    But in the end, it's better to profile application and find bottlenecks for whole application.

提交回复
热议问题