如果我设置了.NET集合的初始大小,然后添加了一些超出此初始大小的项目,那么集合如何确定下一个调整大小? [英] If I set the initial size of a .NET collection and then add some items OVER this initial size, how does the collection determine the next resize?
问题描述
当我们知道一些预期的最大大小时,我就开始尝试设置某些.NET集合的初始大小.思考,静态数据预缓存等.
I've started trying to set the initial size of some of my .NET collections when we know some expected max sizes. Think, pre-caching of static data etc.
如果我们稍后开始向该集合中添加项目,则该集合将调整大小以适合这些新项目.它如何确定下一个调整大小?我以为,如果我们使用默认逻辑并且不设置初始大小,它将根据一些预编码的逻辑来调整大小.现在我们已经设置了初始大小,这会破坏逻辑吗?
If we start adding items to this collection later on the collection will resize to fit these new items in. How does it determine what the next resize will be? I thought that, if we use the default logic and don't set an initial size, it will resize based on some pre-coded logic. Now that we're set the initial size, does this screw that logic up?
它是否简单化,它是一些数学公式,例如当前大小+ 10%
或类似的东西?
is it simplistic and it's some math formula like current size + 10%
or something?
推荐答案
列表中的< T>在实施中,算法是将大小从4开始,一旦达到大小,则将其加倍.最佳实践是分配List< T>.与施工时的尺寸.如果知道大小,则提前分配大小和内存会更有效.请参见 http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs
In the List <T> implementation, the algorithm is to start the size at 4 and then double it once the size has been reached. The best practice is to allocate a List<T> with the size at construction time. It is more efficient to allocate the size and memory upfront if you know the size. See http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs
这篇关于如果我设置了.NET集合的初始大小,然后添加了一些超出此初始大小的项目,那么集合如何确定下一个调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!