Capacity ide
在.NET中List的容量應該只是受到硬件限制。函數
屬性Capacity的真正含義,是建立List時給它預分配的容量。性能
一旦項的數量超過了當前的Capacity,Capacity會以spa
Capacity = (Capacity==0)?A_VALUE_YOU_CANNOT_SURE:(Capacity*2)3d
的方式增加。code
當使用默認構造函數建立一個List時,Capacity就是0。(或者你非要用一個0做爲capacity參數調用構造)orm
因此當Capacity是0時,咱們爲List添加了第一個項目後,Capacity是多少呢?blog
對,是A_VALUE_YOU_CANNOT_SURE。ci
不過,在.NET2.0和.NET4.5中,這個值看上去都是4。get
System.Diagnostics.Debug.WriteLine(string.Format("default capacity is : {0}", new List<string>().Capacity)); int initCapacity = 0; List<string> l = new List<string>(initCapacity); System.Diagnostics.Debug.WriteLine(string.Format("constructe a list with capacity: {0}", l.Capacity)); for (int u = 0; u < 65; ++u) { System.Diagnostics.Debug.WriteLine(string.Format("Count: {0}, Capacity: {1}", l.Count, l.Capacity)); //l.TrimExcess(); //System.Diagnostics.Debug.WriteLine(string.Format(" TrimExcess() -> Count: {0}, Capacity: {1}", l.Count, l.Capacity)); l.Add("1"); }
輸出
default capacity is : 0 constructe a list with capacity: 0 Count: 0, Capacity: 0 Count: 1, Capacity: 4 Count: 2, Capacity: 4 Count: 3, Capacity: 4 Count: 4, Capacity: 4 Count: 5, Capacity: 8 Count: 6, Capacity: 8 Count: 7, Capacity: 8 Count: 8, Capacity: 8 Count: 9, Capacity: 16 Count: 10, Capacity: 16 Count: 11, Capacity: 16 Count: 12, Capacity: 16 Count: 13, Capacity: 16 Count: 14, Capacity: 16 Count: 15, Capacity: 16 Count: 16, Capacity: 16 Count: 17, Capacity: 32 Count: 18, Capacity: 32 Count: 19, Capacity: 32 Count: 20, Capacity: 32 Count: 21, Capacity: 32 Count: 22, Capacity: 32 Count: 23, Capacity: 32 Count: 24, Capacity: 32 Count: 25, Capacity: 32 Count: 26, Capacity: 32 Count: 27, Capacity: 32 Count: 28, Capacity: 32 Count: 29, Capacity: 32 Count: 30, Capacity: 32 Count: 31, Capacity: 32 Count: 32, Capacity: 32 Count: 33, Capacity: 64 Count: 34, Capacity: 64 Count: 35, Capacity: 64 Count: 36, Capacity: 64 Count: 37, Capacity: 64 Count: 38, Capacity: 64 Count: 39, Capacity: 64 Count: 40, Capacity: 64 Count: 41, Capacity: 64 Count: 42, Capacity: 64 Count: 43, Capacity: 64 Count: 44, Capacity: 64 Count: 45, Capacity: 64 Count: 46, Capacity: 64 Count: 47, Capacity: 64 Count: 48, Capacity: 64 Count: 49, Capacity: 64 Count: 50, Capacity: 64 Count: 51, Capacity: 64 Count: 52, Capacity: 64 Count: 53, Capacity: 64 Count: 54, Capacity: 64 Count: 55, Capacity: 64 Count: 56, Capacity: 64 Count: 57, Capacity: 64 Count: 58, Capacity: 64 Count: 59, Capacity: 64 Count: 60, Capacity: 64 Count: 61, Capacity: 64 Count: 62, Capacity: 64 Count: 63, Capacity: 64 Count: 64, Capacity: 64
TrimExcess()
TrimExcess()的意思就是,若是Capacity大於Count,清掉哪些沒有使用的空間。基本上能夠理解成讓Capacity等於Count,只是基本上,不能信賴這一點。.NET畢竟還要考慮不少性能問題。TrimExcess method does nothing if the list is at more than 90 percent of capacity——這就是.NET的方式。實際上把上面那段代碼中的註釋放開,就會看到確實存在Capacity和Count不等的狀況(然而,並非90%喲)。Capacity不夠之後的增加規則,仍然是*2,並不會由於被Trim過就只是加1。
放開註釋後的輸出
default capacity is : 0 constructe a list with capacity: 0 Count: 0, Capacity: 0 TrimExcess() -> Count: 0, Capacity: 0 Count: 1, Capacity: 4 TrimExcess() -> Count: 1, Capacity: 1 Count: 2, Capacity: 2 TrimExcess() -> Count: 2, Capacity: 2 Count: 3, Capacity: 4 TrimExcess() -> Count: 3, Capacity: 4 Count: 4, Capacity: 4 TrimExcess() -> Count: 4, Capacity: 4 Count: 5, Capacity: 8 TrimExcess() -> Count: 5, Capacity: 5 Count: 6, Capacity: 10 TrimExcess() -> Count: 6, Capacity: 6 Count: 7, Capacity: 12 TrimExcess() -> Count: 7, Capacity: 7 Count: 8, Capacity: 14 TrimExcess() -> Count: 8, Capacity: 8 Count: 9, Capacity: 16 TrimExcess() -> Count: 9, Capacity: 9 Count: 10, Capacity: 18 TrimExcess() -> Count: 10, Capacity: 10 Count: 11, Capacity: 20 TrimExcess() -> Count: 11, Capacity: 11 Count: 12, Capacity: 22 TrimExcess() -> Count: 12, Capacity: 12 Count: 13, Capacity: 24 TrimExcess() -> Count: 13, Capacity: 13 Count: 14, Capacity: 26 TrimExcess() -> Count: 14, Capacity: 14 Count: 15, Capacity: 28 TrimExcess() -> Count: 15, Capacity: 15 Count: 16, Capacity: 30 TrimExcess() -> Count: 16, Capacity: 16 Count: 17, Capacity: 32 TrimExcess() -> Count: 17, Capacity: 17 Count: 18, Capacity: 34 TrimExcess() -> Count: 18, Capacity: 18 Count: 19, Capacity: 36 TrimExcess() -> Count: 19, Capacity: 19 Count: 20, Capacity: 38 TrimExcess() -> Count: 20, Capacity: 20 Count: 21, Capacity: 40 TrimExcess() -> Count: 21, Capacity: 21 Count: 22, Capacity: 42 TrimExcess() -> Count: 22, Capacity: 22 Count: 23, Capacity: 44 TrimExcess() -> Count: 23, Capacity: 23 Count: 24, Capacity: 46 TrimExcess() -> Count: 24, Capacity: 24 Count: 25, Capacity: 48 TrimExcess() -> Count: 25, Capacity: 25 Count: 26, Capacity: 50 TrimExcess() -> Count: 26, Capacity: 26 Count: 27, Capacity: 52 TrimExcess() -> Count: 27, Capacity: 27 Count: 28, Capacity: 54 TrimExcess() -> Count: 28, Capacity: 28 Count: 29, Capacity: 56 TrimExcess() -> Count: 29, Capacity: 29 Count: 30, Capacity: 58 TrimExcess() -> Count: 30, Capacity: 30 Count: 31, Capacity: 60 TrimExcess() -> Count: 31, Capacity: 31 Count: 32, Capacity: 62 TrimExcess() -> Count: 32, Capacity: 32 Count: 33, Capacity: 64 TrimExcess() -> Count: 33, Capacity: 33 Count: 34, Capacity: 66 TrimExcess() -> Count: 34, Capacity: 34 Count: 35, Capacity: 68 TrimExcess() -> Count: 35, Capacity: 35 Count: 36, Capacity: 70 TrimExcess() -> Count: 36, Capacity: 36 Count: 37, Capacity: 72 TrimExcess() -> Count: 37, Capacity: 37 Count: 38, Capacity: 74 TrimExcess() -> Count: 38, Capacity: 38 Count: 39, Capacity: 76 TrimExcess() -> Count: 39, Capacity: 39 Count: 40, Capacity: 78 TrimExcess() -> Count: 40, Capacity: 40 Count: 41, Capacity: 80 TrimExcess() -> Count: 41, Capacity: 41 Count: 42, Capacity: 82 TrimExcess() -> Count: 42, Capacity: 42 Count: 43, Capacity: 84 TrimExcess() -> Count: 43, Capacity: 43 Count: 44, Capacity: 86 TrimExcess() -> Count: 44, Capacity: 44 Count: 45, Capacity: 88 TrimExcess() -> Count: 45, Capacity: 45 Count: 46, Capacity: 90 TrimExcess() -> Count: 46, Capacity: 46 Count: 47, Capacity: 92 TrimExcess() -> Count: 47, Capacity: 47 Count: 48, Capacity: 94 TrimExcess() -> Count: 48, Capacity: 48 Count: 49, Capacity: 96 TrimExcess() -> Count: 49, Capacity: 49 Count: 50, Capacity: 98 TrimExcess() -> Count: 50, Capacity: 50 Count: 51, Capacity: 100 TrimExcess() -> Count: 51, Capacity: 51 Count: 52, Capacity: 102 TrimExcess() -> Count: 52, Capacity: 52 Count: 53, Capacity: 104 TrimExcess() -> Count: 53, Capacity: 53 Count: 54, Capacity: 106 TrimExcess() -> Count: 54, Capacity: 54 Count: 55, Capacity: 108 TrimExcess() -> Count: 55, Capacity: 55 Count: 56, Capacity: 110 TrimExcess() -> Count: 56, Capacity: 56 Count: 57, Capacity: 112 TrimExcess() -> Count: 57, Capacity: 57 Count: 58, Capacity: 114 TrimExcess() -> Count: 58, Capacity: 58 Count: 59, Capacity: 116 TrimExcess() -> Count: 59, Capacity: 59 Count: 60, Capacity: 118 TrimExcess() -> Count: 60, Capacity: 60 Count: 61, Capacity: 120 TrimExcess() -> Count: 61, Capacity: 61 Count: 62, Capacity: 122 TrimExcess() -> Count: 62, Capacity: 62 Count: 63, Capacity: 124 TrimExcess() -> Count: 63, Capacity: 63 Count: 64, Capacity: 126 TrimExcess() -> Count: 64, Capacity: 64
發現並非90%之後
System.Diagnostics.Debug.WriteLine(string.Format("default capacity is : {0}", new List<string>().Capacity)); int initCapacity = 100; List<string> l = new List<string>(initCapacity); System.Diagnostics.Debug.WriteLine(string.Format("constructe a list with capacity: {0}", l.Capacity)); int initCount = 100; for (int u = 0; u < initCount; ++u) { l.Add(u.ToString()); } for (int u = 0; u < initCount; ++u) { System.Diagnostics.Debug.WriteLine(string.Format("Count: {0}, Capacity: {1}", l.Count, l.Capacity)); l.TrimExcess(); System.Diagnostics.Debug.WriteLine(string.Format(" TrimExcess() -> Count: {0}, Capacity: {1}", l.Count, l.Capacity)); l.Remove(u.ToString()); }
獲得
default capacity is : 0 constructe a list with capacity: 100 Count: 100, Capacity: 100 TrimExcess() -> Count: 100, Capacity: 100 Count: 99, Capacity: 100 TrimExcess() -> Count: 99, Capacity: 100 Count: 98, Capacity: 100 TrimExcess() -> Count: 98, Capacity: 100 Count: 97, Capacity: 100 TrimExcess() -> Count: 97, Capacity: 100 Count: 96, Capacity: 100 TrimExcess() -> Count: 96, Capacity: 100 Count: 95, Capacity: 100 TrimExcess() -> Count: 95, Capacity: 100 Count: 94, Capacity: 100 TrimExcess() -> Count: 94, Capacity: 100 Count: 93, Capacity: 100 TrimExcess() -> Count: 93, Capacity: 100 Count: 92, Capacity: 100 TrimExcess() -> Count: 92, Capacity: 100 Count: 91, Capacity: 100 TrimExcess() -> Count: 91, Capacity: 100 Count: 90, Capacity: 100 TrimExcess() -> Count: 90, Capacity: 100 Count: 89, Capacity: 100 TrimExcess() -> Count: 89, Capacity: 89 Count: 88, Capacity: 89 TrimExcess() -> Count: 88, Capacity: 89 Count: 87, Capacity: 89 TrimExcess() -> Count: 87, Capacity: 89 Count: 86, Capacity: 89 TrimExcess() -> Count: 86, Capacity: 89 Count: 85, Capacity: 89 TrimExcess() -> Count: 85, Capacity: 89 Count: 84, Capacity: 89 TrimExcess() -> Count: 84, Capacity: 89 Count: 83, Capacity: 89 TrimExcess() -> Count: 83, Capacity: 89 Count: 82, Capacity: 89 TrimExcess() -> Count: 82, Capacity: 89 Count: 81, Capacity: 89 TrimExcess() -> Count: 81, Capacity: 89 Count: 80, Capacity: 89 TrimExcess() -> Count: 80, Capacity: 89 Count: 79, Capacity: 89 TrimExcess() -> Count: 79, Capacity: 79 Count: 78, Capacity: 79 TrimExcess() -> Count: 78, Capacity: 79 Count: 77, Capacity: 79 TrimExcess() -> Count: 77, Capacity: 79 Count: 76, Capacity: 79 TrimExcess() -> Count: 76, Capacity: 79 Count: 75, Capacity: 79 TrimExcess() -> Count: 75, Capacity: 79 Count: 74, Capacity: 79 TrimExcess() -> Count: 74, Capacity: 79 Count: 73, Capacity: 79 TrimExcess() -> Count: 73, Capacity: 79 Count: 72, Capacity: 79 TrimExcess() -> Count: 72, Capacity: 79 Count: 71, Capacity: 79 TrimExcess() -> Count: 71, Capacity: 79 Count: 70, Capacity: 79 TrimExcess() -> Count: 70, Capacity: 70 Count: 69, Capacity: 70 TrimExcess() -> Count: 69, Capacity: 70 Count: 68, Capacity: 70 TrimExcess() -> Count: 68, Capacity: 70 Count: 67, Capacity: 70 TrimExcess() -> Count: 67, Capacity: 70 Count: 66, Capacity: 70 TrimExcess() -> Count: 66, Capacity: 70 Count: 65, Capacity: 70 TrimExcess() -> Count: 65, Capacity: 70 Count: 64, Capacity: 70 TrimExcess() -> Count: 64, Capacity: 70 Count: 63, Capacity: 70 TrimExcess() -> Count: 63, Capacity: 70 Count: 62, Capacity: 70 TrimExcess() -> Count: 62, Capacity: 62 Count: 61, Capacity: 62 TrimExcess() -> Count: 61, Capacity: 62 Count: 60, Capacity: 62 TrimExcess() -> Count: 60, Capacity: 62 Count: 59, Capacity: 62 TrimExcess() -> Count: 59, Capacity: 62 Count: 58, Capacity: 62 TrimExcess() -> Count: 58, Capacity: 62 Count: 57, Capacity: 62 TrimExcess() -> Count: 57, Capacity: 62 Count: 56, Capacity: 62 TrimExcess() -> Count: 56, Capacity: 62 Count: 55, Capacity: 62 TrimExcess() -> Count: 55, Capacity: 62 Count: 54, Capacity: 62 TrimExcess() -> Count: 54, Capacity: 54 Count: 53, Capacity: 54 TrimExcess() -> Count: 53, Capacity: 54 Count: 52, Capacity: 54 TrimExcess() -> Count: 52, Capacity: 54 Count: 51, Capacity: 54 TrimExcess() -> Count: 51, Capacity: 54 Count: 50, Capacity: 54 TrimExcess() -> Count: 50, Capacity: 54 Count: 49, Capacity: 54 TrimExcess() -> Count: 49, Capacity: 54 Count: 48, Capacity: 54 TrimExcess() -> Count: 48, Capacity: 54 Count: 47, Capacity: 54 TrimExcess() -> Count: 47, Capacity: 47 Count: 46, Capacity: 47 TrimExcess() -> Count: 46, Capacity: 47 Count: 45, Capacity: 47 TrimExcess() -> Count: 45, Capacity: 47 Count: 44, Capacity: 47 TrimExcess() -> Count: 44, Capacity: 47 Count: 43, Capacity: 47 TrimExcess() -> Count: 43, Capacity: 47 Count: 42, Capacity: 47 TrimExcess() -> Count: 42, Capacity: 47 Count: 41, Capacity: 47 TrimExcess() -> Count: 41, Capacity: 41 Count: 40, Capacity: 41 TrimExcess() -> Count: 40, Capacity: 41 Count: 39, Capacity: 41 TrimExcess() -> Count: 39, Capacity: 41 Count: 38, Capacity: 41 TrimExcess() -> Count: 38, Capacity: 41 Count: 37, Capacity: 41 TrimExcess() -> Count: 37, Capacity: 41 Count: 36, Capacity: 41 TrimExcess() -> Count: 36, Capacity: 41 Count: 35, Capacity: 41 TrimExcess() -> Count: 35, Capacity: 35 Count: 34, Capacity: 35 TrimExcess() -> Count: 34, Capacity: 35 Count: 33, Capacity: 35 TrimExcess() -> Count: 33, Capacity: 35 Count: 32, Capacity: 35 TrimExcess() -> Count: 32, Capacity: 35 Count: 31, Capacity: 35 TrimExcess() -> Count: 31, Capacity: 35 Count: 30, Capacity: 35 TrimExcess() -> Count: 30, Capacity: 30 Count: 29, Capacity: 30 TrimExcess() -> Count: 29, Capacity: 30 Count: 28, Capacity: 30 TrimExcess() -> Count: 28, Capacity: 30 Count: 27, Capacity: 30 TrimExcess() -> Count: 27, Capacity: 30 Count: 26, Capacity: 30 TrimExcess() -> Count: 26, Capacity: 26 Count: 25, Capacity: 26 TrimExcess() -> Count: 25, Capacity: 26 Count: 24, Capacity: 26 TrimExcess() -> Count: 24, Capacity: 26 Count: 23, Capacity: 26 TrimExcess() -> Count: 23, Capacity: 26 Count: 22, Capacity: 26 TrimExcess() -> Count: 22, Capacity: 22 Count: 21, Capacity: 22 TrimExcess() -> Count: 21, Capacity: 22 Count: 20, Capacity: 22 TrimExcess() -> Count: 20, Capacity: 22 Count: 19, Capacity: 22 TrimExcess() -> Count: 19, Capacity: 22 Count: 18, Capacity: 22 TrimExcess() -> Count: 18, Capacity: 18 Count: 17, Capacity: 18 TrimExcess() -> Count: 17, Capacity: 18 Count: 16, Capacity: 18 TrimExcess() -> Count: 16, Capacity: 18 Count: 15, Capacity: 18 TrimExcess() -> Count: 15, Capacity: 15 Count: 14, Capacity: 15 TrimExcess() -> Count: 14, Capacity: 15 Count: 13, Capacity: 15 TrimExcess() -> Count: 13, Capacity: 15 Count: 12, Capacity: 15 TrimExcess() -> Count: 12, Capacity: 12 Count: 11, Capacity: 12 TrimExcess() -> Count: 11, Capacity: 12 Count: 10, Capacity: 12 TrimExcess() -> Count: 10, Capacity: 12 Count: 9, Capacity: 12 TrimExcess() -> Count: 9, Capacity: 9 Count: 8, Capacity: 9 TrimExcess() -> Count: 8, Capacity: 9 Count: 7, Capacity: 9 TrimExcess() -> Count: 7, Capacity: 7 Count: 6, Capacity: 7 TrimExcess() -> Count: 6, Capacity: 7 Count: 5, Capacity: 7 TrimExcess() -> Count: 5, Capacity: 5 Count: 4, Capacity: 5 TrimExcess() -> Count: 4, Capacity: 5 Count: 3, Capacity: 5 TrimExcess() -> Count: 3, Capacity: 3 Count: 2, Capacity: 3 TrimExcess() -> Count: 2, Capacity: 3 Count: 1, Capacity: 3 TrimExcess() -> Count: 1, Capacity: 1
貌似條件是醬紫的
if( count<(int)Math.Floor((double)capacity*0.9) )
do trim
我也不知道我要幹嗎