微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#List <>我应该在删除项目时降低容量吗?

我有一个List容器,最开始可能包含100,000个项目.当程序运行时,此列表将慢慢清空,我是否应该在清空列表时更改容量?

我做了一些测试,执行时间似乎是一样的,但降低列表容量有多少开销?我可以找到很多关于增加容量的信息,但没有太多关于降低容量的信息.

解决方法

除非你的内存量非常低,否则这是一个微优化.

通常,无需更改List<>的容量.

TrimExcess方法文档:

This method can be used to minimize a collection’s memory overhead if no new elements will be added to the collection. The cost of reallocating and copying a large List<T> can be considerable,however,so the TrimExcess method does nothing if the list is at more than 90 percent of capacity. This avoids incurring a large reallocation cost for a relatively small gain.

原文地址:https://www.jb51.cc/c/115964.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐