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.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...